Simsevu
Simsevu

Reputation: 200

Redirect www to non-www is loosing my other RewriteRule

I have this code in my, .htaccess file.

My url is fine foodzite.dk/vis/1234567 When i enter www.foodzite.dk/vis/1234567 i want it to redirect to foodzite.dk/vis/1234567 but i redirects to foodzite.dk/shop.php?url=1234567

How is it i cant combine the to Rewrites?

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]


RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]

Upvotes: 0

Views: 45

Answers (2)

Jon Lin
Jon Lin

Reputation: 143886

You need to have your redirect rule before your routing rule. Otherwise the URI gets routed to your php file, then the redirect rule gets applied and the URI is exposed:

Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]


RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]


RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]


RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]

Additionally, your conditions only get applied to the immediately following rewrite rule, so the 2nd and 3rd don't have the same conditions. (Unless you wanted it to be that way).

Upvotes: 1

Xorandnotor
Xorandnotor

Reputation: 156

Try something like:

RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^foodzite.dk [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Upvotes: 0

Related Questions