Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess rewrite rule bugging. can't establish why

10:56 am on May 24, 2007 (gmt 0)

5+ Year Member

Hello, having a problem with seperating multiple string variables.

Started out with:

Options +FollowSymLinks -MultiViews

RewriteEngine on

RewriteRule ^(.*)/$ http://www.website.com/index.php?id=$1 [NC]

RewriteCond %{REQUEST_URI}!(\.[^/]*/)$ 

RewriteRule (.*) http://www.website.com/$1/ [R=302,L]

RewriteRule ^http://www.website.com/news/(.*)/$ http://www.website.com/index.php?id=news&newspage=$1 [NC]

RewriteCond %{REQUEST_URI}!(\.[^/]*/)$ 

RewriteRule (.*) http://www.website.com/news/$1/ [R=302,L] 

ErrorDocument 404 http://www.website.com/index.php?id=404 [NC]

my news page has some automated pagination and loads page content specified in the string - I wrote the line

RewriteRule ^http://www.website.com/news/(.*)/$ http://www.website.com/index.php?id=news&newspage=$1 [NC]

[website.com...] should load page 2 of news
but what I'm seeing is the custom 404 I've made which means its not seperating the string correctly. (checked the error log and no error)

[website.com...] works correctly.

if the rule is written as

RewriteRule ^(.*)/(.*)/$ http://www.website.com/index.php?id=news&newspage=$2 [NC]

[website.com...] loads the correct data but [website.com...] or [website.com...] give sql errors (to do with the handling of the string variables)

I'm struggling to write anything thats good for both

1:05 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

At first glance, I'd say you're a victim of the greedy and ambiguous ".*" regex pattern here.

A pattern of ^(.*)/$ as specified in your first rule will match any and all of these:

So, your first rule catches everything, giving a script call of /index.php?id=news/2

I suggest putting your script rewriting rules in order from most-specific to least specific, and using unambiguous regex patterns wherever possible -- and that means "most of the time." -- In addition to preventing unexpected matches and the resulting problems, they also offer a very-significant processing-speed advantage in many cases.

Use the [L] flag on every RewriteRule, unless you have a good reason not to do so.

Your ErrorDocument directive syntax is incorrect, and will result in a 302 redirect instead of a 404 response -- See the ErrorDocument directive in the Apache core directives documentation.

Also, you'll be much happier with yourself 3 years from now if you comment your code today. Trust me on this one... :)

I suggest:

# Specify custom error documents
ErrorDocument 404 [b]/i[/b]ndex.php?id=404 [NC]
# Enable mod_rewrite
Options +FollowSymLinks -MultiViews
RewriteEngine on
# If no filetype specified in final URL-path-part,
# Externally redirect to append missing trailing slash
RewriteCond %{REQUEST_URI} !(\.[^/]*/)$
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]
# Internally rewrite SE-friendly news+parameter URLs to script
RewriteRule ^news/([^/]+)/$ /index.php?id=news&newspage=$1 [NC,L]
# Internally rewrite remaining single-parameter SE-friendly URLs to script
RewriteRule ^([^/]+)/$ /index.php?id=$1 [L]
# -end-

Replace the broken pipe "" character above with a solid pipe before use; Posting on this forum modifies the pipe characters.


3:03 pm on May 24, 2007 (gmt 0)

5+ Year Member

thats great jim

still took a bit of fiddling but it works a treat

really strange but I have problems with htaccess if I include comments - doesn't work til I strip them all - bit of a pain!

7:28 pm on May 24, 2007 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Comments must be on a line by themselves to avoid Apache "Warnings," but otherwise should not cause problems. I'd suggest having your host look into this.



Featured Threads

Hot Threads This Week

Hot Threads This Month