Author Topic: Rewrite  (Read 2784 times)

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Rewrite
« on: May 02, 2011, 10:19:20 PM »
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/(.*)$ index.php?req=$1&action=$2&obj=$3 [NC,L]

I need that rule to work unless the 3rd portion ends in .js.  In that case, the rule needs to be ignored.  Help?  Obviously I'm keying on everything in that last part but I basically need it to process everything except .js files.

PJYelton

  • Power of Voodoo
  • Jackass In Charge
  • Posts: 1597
  • Karma: +56/-12
    • TheRecursiveFractal
Re: Rewrite
« Reply #1 on: May 02, 2011, 10:25:32 PM »
Wait, .js at the very end or .js in the (.*) right after the second slash?

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Rewrite
« Reply #2 on: May 02, 2011, 10:31:32 PM »
Use RewriteCond first

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Rewrite
« Reply #3 on: May 02, 2011, 10:51:54 PM »
Mike, I'll look into that, but just so I can clarify... I have multiple rules:

RewriteRule ^([a-z]+)/([a-z]+)/([0-9]+)$ index.php?req=$1&action=$2&obj=$3 [NC,L]
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/(.*)$ index.php?req=$1&action=$2&obj=$3 [NC,L]
RewriteRule ^([a-z]+)/([0-9]+)$ index.php?req=$1&action=$2 [NC,L]
RewriteRule ^([a-z]+)/([a-z]+)$ index.php?req=$1&action=$2 [NC,L]
RewriteRule ^([a-z]+)$ index.php?req=$1 [NC,L]

Unfortunately, my js includes look like this:

<script type="text/javascript" src="http://www.blah.com/dev/sources/account/invacct.js"></script>

And it's picking up the rewrite on those and causing problems.

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Rewrite
« Reply #4 on: May 02, 2011, 10:55:41 PM »
Thanks Mike!

Code: [Select]
RewriteRule ^([a-z]+)/([a-z]+)/([0-9]+)$ index.php?req=$1&action=$2&obj=$3 [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+)/([a-zA-Z0-9]+)/(.*)$ index.php?req=$1&action=$2&obj=$3 [NC,L]
RewriteRule ^([a-z]+)/([0-9]+)$ index.php?req=$1&action=$2 [NC,L]
RewriteRule ^([a-z]+)/([a-z]+)$ index.php?req=$1&action=$2 [NC,L]
RewriteRule ^([a-z]+)$ index.php?req=$1 [NC,L]

PJYelton

  • Power of Voodoo
  • Jackass In Charge
  • Posts: 1597
  • Karma: +56/-12
    • TheRecursiveFractal
Re: Rewrite
« Reply #5 on: May 02, 2011, 11:06:36 PM »
Not familiar with what I am guessing is php but I am familiar with regex.  If you still need regex than you'll need to look at the negative lookahead operator. 

For example the code to match a period that is not followed by js would be \.(?!js)

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Rewrite
« Reply #6 on: May 02, 2011, 11:14:01 PM »
Not familiar with what I am guessing is php but I am familiar with regex.  If you still need regex than you'll need to look at the negative lookahead operator. 

For example the code to match a period that is not followed by js would be \.(?!js)
Those are Apache rewrite rules.

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: Rewrite
« Reply #7 on: May 03, 2011, 07:24:47 AM »
it looks like you solved this already but I started typing my reply before I read the rest of this thread so I'm still posting this

I always add this to my htaccess before the rewrite:

Code: [Select]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

that way, any files that actaully exist will load normally, regardless of the rewrite rule; necessary for things like images, css and js files.
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

ober

  • Ashton Shagger
  • Ass Wipe
  • Posts: 14310
  • Karma: +73/-790
  • mini-ober is taking over
    • Windy Hill Web Solutions
Re: Rewrite
« Reply #8 on: May 03, 2011, 08:57:36 AM »
The only question I had was if I put those 2 lines above all of the rules, does it take effect for all of them or do I have to put them above each individual rule?

micah

  • A real person, on the Internet.
  • Ass Wipe
  • Posts: 6915
  • Karma: +58/-55
  • Truth cannot contradict truth.
    • micahj.com
Re: Rewrite
« Reply #9 on: May 03, 2011, 09:16:18 AM »
I *think* rewrite conditions happen in order, then it "breaks" (in the "stop processing" sense of the word) once its met a condition.

so if an existing file or directory are found using those lines, it won't run any of the other conditions after it.

hopefully someone will correct me if I'm wrong.
"I possess a device, in my pocket, that is capable of accessing the entirety of information known to man.  I use it to look at pictures of cats and get in arguments with strangers."

Mike

  • Jackass In Charge
  • Posts: 11257
  • Karma: +168/-32
  • Ex Asshole - a better and more caring person.
Re: Rewrite
« Reply #10 on: May 03, 2011, 10:07:10 AM »
The only question I had was if I put those 2 lines above all of the rules, does it take effect for all of them or do I have to put them above each individual rule?
I'm 99.99% sure you can put them before any of the rules and it'll effect all of them.