Forum Moderators: phranque
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
A pattern of ^(.*)/$ as specified in your first rule will match any and all of these:
news/
news/2/
blues/news/2/
world/blues/news/222/
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-
Jim