Mitya
Mitya

Reputation: 34576

Why is this .htaccess not silently redirecting?

Can anyone tell me why the following visibly changes the URL in the browser, rather than redirecting silently? The redirect works, but visibly.

Example URL: http://domain.com/_test/sportswire/uk/football/huddersfield

...visibily redirects with query string arguments showing.

RewriteEngine on
RewriteBase /_test/sportswire

#favour naked domain over www
RewriteCond %{HTTP_HOST} ^www\.(.+) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,NE,L]

#disallow trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

###THIS RULE### channel pages - territory/sport/channel/[res type?]
RewriteRule ^(\w+)\/(\w+)\/(\w+)(?:\/(\w+))?$ channel.php?territory=$1&sport=$2&team=$3&res_type=$4 [L,NC,R=301]

#channel pages - with item (if from external referrer) - territory/sport/channel/res_type/"item"/item_id/pretty
RewriteRule ^(\w+)\/(\w+)\/(\w+)\/(\w+)\/story\/\w{11}\/[^\/]+$ channel.php?territory=$1&sport=$2&team=$3&res_type=$4&item=$5 [L,NC,R=301]

What I've tried/read:

I'm fairly convinced there's nothing on planet earth quite as perplexing as mod-rewrite.

Upvotes: 0

Views: 147

Answers (2)

BeetleJuice
BeetleJuice

Reputation: 40916

Try removing the [R] flag altogether, so that the server won't notify the browser of the change.

htaccess changes take effect immediately, so you don't need to restart the server. However, since you previously tried R=301, your browser may remember to go straight to the URL with the query string and never even ask the server for the URL you're trying to redirect. To see the effect of the change I recommended, start a new browser instance in incognito or private browsing mode and test the new rule.

Side note: why are you escaping all the / in your paths? I don't think you need to.

Upvotes: 1

exabyssus
exabyssus

Reputation: 441

[R] flag should not be there. Try restarting Apache if that is possible, I've noticed that it helps sometimes. Try another browser or different URL, they can cache redirects.

Upvotes: 1

Related Questions