Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite rule works local, does not work remote

.htaccess and mod_rewrite quirks

         

BreathinBuddah

5:05 am on Mar 6, 2011 (gmt 0)

10+ Year Member



Aight, so I have this site, lets call it [mysite.com...]

I have a rewrite rule to change [mysite.com...] into [mysite.com...] (or [mysite.com...] or [mysite.com...] )

.htaccess:
ErrorDocument 404 http://www.mysite.com/404.html
options +FollowSymlinks
rewriteEngine on
rewriteCond %{REQUEST_URI} !-f
rewriteCond %{REQUEST_URI} !-d
rewriteCond %{REQUEST_URI} !index\.php
rewriteRule ^/?([^/]+?)?/?([0-9]+?)/([0-9]+?)$ index.php/%{THE_REQUEST} [NC]


Now, this works on my local apache 2.2.11 server, no errors. However on my host's apache 1.3.41 server, I get the following error:

[Sat Mar 5 21:42:14 2011] [alert] [client [ip]] /home/_/public_html/mysite.com/.htaccess: RewriteRule: cannot compile regular expression '^/?([^/]+?)?/?([0-9]+?)/([0-9]+?)$'\n


I imagine it's something quirky about the apache version as other sites on this host use mod_rewrite without a hitch.

I've tried removing the +followSymlinks line, even the rewrite engine line for gits and shiggles. I haven't tried removing the conditions cause I don't think i should have to.

H4LP!

jdMorgan

5:40 pm on Mar 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The bolded "?" appears to be out of place. It looks like you're attempting to quantify the preceding "+" which is also a quantifier...

rewriteRule ^/?([^/]+?)?/?([0-9]+?)/([0-9]+?)$ index.php/%{THE_REQUEST} [NC]

The code has many other problems as well -- quantifier precedence being quite ambiguous in particular.

Rather than using multiple and un-prioritized "?" quantifiers trying to make bits and pieces of the requested URL-path optional, I suggest that you code this as a series of rules, each one *requiring* a certain number of parameters and slashes in the requested URL, and rewriting only valid URLs to your script. URLs which are missing slashes or have other defects should be detected and either redirected to the correct URLs, or allowed to return a 404-Not Found.

This will also reduce the number of exclusions you'll need to avoid recursion. (Note that your rule will rewrite /x/y to index.php/<request-line> and that the resulting index.php/<request-line> will then get rewritten to itself -- creating an infinite loop). Easy enough to prevent on the two-parameter version of this rule with a preceding RewriteCond:

RewriteCond %{REQUEST_URI} !/index\.php

Your ErrorDocument directive will cause massive problems with search ranking. As documented, the syntax you've used results in a 302-Found redirect to the 404 error page, so your server will never return a proper 404 status code for any requested URLs which do not resolve to files. Therefore, search engines will see an 'infinite URL-space' on your server, and will arbitrarily limit the depth to which they are willing to crawl your site.

The solution is simple. Use
ErrorDocument 404 /404.html

instead. And make it a habit to look up each Apache configuration directive and read the documentation before you use it. Otherwise, there are plenty of similar "SEO-suicide" traps waiting for you. It would be a pity to have a single typo or a small error literally put you out of business before you even get started...

Jim

BreathinBuddah

8:56 pm on Mar 9, 2011 (gmt 0)

10+ Year Member



Thank you so much for your detailed response, you've pointed me in the right direction and I've got it working on both local and remote servers. FYI, I linked this thread to my stack overflow post [stackoverflow.com] as well.

thank you twenty more times.