Forum Moderators: phranque
My final question (of the day) is...
I can do the 301 redirect, I can do the URL rewrite, but I'm having trouble putting them together.
Current code=
RewriteCond %{QUERY_STRING} ^ca=([0-9]+)
RewriteRule ^subdir/$ http://www.example.com/subdir/%1? [R=301,L]
RewriteRule ^subdir/([0-9]+) /subdir/?ca=$1 [L]
So the rewrite rule by itself seems to work, as does the redirect, but put them together and it doesn't. Any thoughts?
Much abliged!
CWebguy
[edited by: CWebguy at 5:27 pm (utc) on Mar. 10, 2009]
# Externally redirect only direct client requests for subdir+query-string URL back to static URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /subdir/\?ca=([0-9]+)\ HTTP/
RewriteRule ^subdir/$ http://www.example.com/subdir/%1? [R=301,L]
#
# Internally rewrite requests for static URL to query string filepath
RewriteRule ^subdir/([0-9]+) /subdir/?ca=$1 [L]
[edited by: jdMorgan at 7:23 pm (utc) on Mar. 11, 2009]
Final code is:
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /store/?ca=([0-9]+)\ HTTP/
RewriteRule ^store/$ http://www.example.com/store/%1? [R=301,L]
RewriteRule ^store/([0-9]+)$ /store/?ca=$1 [L]
#RewriteRule ^store/([0-9]+)/([a-zA-Z0-9]+)$ store/it.php?ca=$1&as=$2 [L]
#RewriteRule ^store/([0-9]+)/pg-([0-9]+)$ /store/?ca=$1&pg=$2 [L]
Thanks.
[edited by: CWebguy at 4:29 pm (utc) on Mar. 11, 2009]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /sto[b]re/\?ca[/b]=([0-9]+)\ HTTP/
RewriteRule ^store/$ http://www.example.com/store/%1? [R=301,L]
The literal question mark in the RewriteCond pattern needs to be escaped as shown, otherwise it will be taken as a regular-expressions quantifier meaning, "zero or one of the preceding character."RewriteCond %{THE_REQUEST} ^[A-Z]+\ /store/\?ca=([0-9]+)\ HTTP/
RewriteRule ^store/$ http://www.example.com/store/%1? [R=301,L]
Thanks a million.
CWebguy
[edited by: CWebguy at 8:02 pm (utc) on Mar. 11, 2009]
Think what happens for a non-www request with the ?ca parameter included.
For that, there will be a double redirect: first the non-www to www redirect, and then the parameter to static redirect kicks in. That Redirection Chain is a BAD thing.
Changing the order of the code around fixes that. With the parameter redirect first, any non-www request with the ?ca parameter included is redirected to static format and the www is fixed at the same time.
That is, the following non-www to www redirect kicks in only for all other requests.