Forum Moderators: phranque
I'm running a trailing slash webpage, and have come across a few problems with my rewriting.
First of all I use a standard code to redirect all traffic from *.domain.com to www.domain.com:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
I then have a set of internal rewrites:
RewriteRule ^/news/$ root/index.php [L]
RewriteRule ^/ajax/$ root/ajax.php [L]
RewriteRule ^/member/$ root/member.php [L]
RewriteRule ^/member/(.+)/$ root/member.php?user=$1 [L]
RewriteRule ^/member/(.+)/(.*)/$ root/member/$1/ [L,R=301]
etc... :)
I then redirect all user requests for .php extensions:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?]*\.php[^&\ ]*\ HTTP/
RewriteRule (.*) root/ [R=301,L]
I then add a trailing slash on every url that doesn't have one:
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}¦/)$
RewriteRule (.*)$ root/$1/ [R=301,L]
--
Now to my problem, everything is working fine except 2 things.
root/member.php?user=john redirects to root/?user=john Any way to have these two redirected to their respective location? (root/) Thanks in advance! [1][edited by: jdMorgan at 10:34 pm (utc) on June 1, 2009]
[2] root/?user=john does no redirect
kraaness
[edit reason] example.com [/edit]
Second, although you've listed two 'problems,' you have not described what is wrong. We don't know what the expected/desired dispostion of these two URLs should be.
Your ".php extensions redirect rule," although flawed, should redirect both of these URLs to root, although you did not clear the query string, so it will still be there.
Every rule in this file has a flaw, but let's address your 'main' problem first. What happens if you delete or comment-put everything else, and just test your two "problem URLs" against this modified rule:
# redirect all user requests for .php extensions:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^.\ ]+\.php[?]?[^&\ ]*\ HTTP/
RewriteRule ^(.+\.php)$ http://www.example.com/root/? [R=301,L]
Jim
[edited by: jdMorgan at 9:59 pm (utc) on June 1, 2009]
When I made the example for rewriting I did a mistake, cause they are in a dir called hp/ so the rule I use is:
RewriteRule ^hp/member/$ http://www.example.com/hp/member.php [L]
I commented all rules except the one supplied by you, and it redirects urls with hp/*.php and hp/.php?*, but not urls with hp/?*
Any way to make the .php string optional, like you have with the question mark?
Kim
[edited by: jdMorgan at 10:35 pm (utc) on June 1, 2009]
[edit reason] example.com [/edit]
# redirect all user requests with blank or single name/value pair queries
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?]*\?[^&\ ]*\ HTTP/
RewriteRule ^(.*)$ http://www.example.com/root/? [R=301,L]
Jim
[edited by: jdMorgan at 10:41 pm (utc) on June 1, 2009]
*** I use a standard code to redirect all traffic from *.domain.com to www.domain.com ***
Look carefully again at your code. It doesn't match that description. It matches only example.com. It does not match *.example.com at all. It has other flaws if the request arrives with appended port numbers, etc, and I'm sure Jim will fix that too in the final solution.
I just wanted to mention this one item to reinforce that clear and accurate description of the problem, and the example URLs, is vital. Too many times I have seen perfect code which fixes a different problem than the one that was actually required to be fixed.
My apologies for the complications, not my intention to waste anyones time. I will surely have a bigger readup on regex in the future as this is by far more complex than I imagined :)
Edit: Oh I wasn't aware of that g1smd, but I only need example.com redirected to www.example.com, sorry for yet another typo. If there are flaws in the additional codes I would like to address them however, thanks.
# Redirect all user requests to remove blank or single name/value pair queries
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?]*\?[^&\ ]*\ HTTP/
RewriteRule ^(.*)$ http://www.example.com/root/? [R=301,L]
#
# Redirect to add missing trailing slash if no file extension is present on the URL
RewriteCond %{REQUEST_URI} !(\.[a-z0-9]{1,5}¦/)$ [NC]
RewriteRule ^(.*)$ http://www.example.com/root/$1/ [R=301,L]
#
# Redirect non-canonical hostname requests to the canonical hostname
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com(\.¦\.?:([0-9]+) [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# Alternate (simpler and more robust) rule to replace the one above
# Redirect to canonical hostname
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
#
# internal rewrites:
RewriteRule ^hp/news/$ root/index.php [L]
RewriteRule ^hp/ajax/$ root/ajax.php [L]
RewriteRule ^hp/member/$ root/member.php [L]
RewriteRule ^hp/member/(.+)/$ root/member.php?user=$1 [L]
#
# The following rule was a redirect, not a rewrite. If a redirect is what is desired, then this code
# should precede all internal rewrites. Place it before the domain canonicalization redirect above.
RewriteRule ^hp/member/(.+)/(.*)/$ http://www.example.com/root/member/$1/ [R=301,L]
# ... etc
Jim