Redirect www to non-www is loosing my other RewriteRule - apache

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]

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).

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

Related

How can i redirect one url to another url with htaccess file?

I want redirect from domain.com/temp/xyx/tech-specs to domain.com/temp/xyx/tech-specs/pemf-ts through htaccess. I tried below code but it is not redirecting
RewriteEngine On
RewriteBase /temp/xyx/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^tech-specs$
RewriteRule ^$ tech-specs/pemf-ts [L,R=301]
You are using HTTP_HOST variable but what you need to match is REQUEST_URI. Also keep redirect rule before internal rewrite rule.
Have it like this:
RewriteEngine On
RewriteBase /temp/xyx/
RewriteRule ^(tech-specs)/?$ $1/pemf-ts [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?_route_=$1 [L,QSA]

Using htaccess to redirect all pages but one to another domain

My htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.com/es [L,R=301]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]
Basically it's function is to redirect all domain alias (i.e. domain.mobi) to domain.com, except domain.es, that will be redirected to domain.com/es. Then there's another rewriterule that appends the query string (so in the background, domain.com/test becomes domain.com/index.php?section=test)
My problem is that now I need to exclude a url (www.domain.es/landing) from the redirection (so it stays in domain.es), and I can't make it work. I've tried adding this condition to the first two rules to exclude the page:
RewriteCond %{REQUEST_URI} !^/landing$
but then it goes to www.domain.com/es?section=landing (not only still redirects to domain.com; the query string appears in the browser bar). Any ideas?
You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.es$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/es/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

redirection all https requests to http

Small problem here, before changing my site to CMS, I have encountered a problem with redirecting all https:// requests to http://.
I have had the following in my .htaccess, which was doing all that
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
but after changing it to CMS that requires different setup, I can no longer use the above mentioned code, since it does not do what it was intended to do in the first place.
Here is my current setup from .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [QSA,L,NC]
RewriteCond %{HTTP_HOST} !^(www\.)mysite\.net$ [NC]
RewriteRule ^(.*)$ http://www.mysite.net/$1 [L,R=301]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.net/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F]
Can you please advise me on how to do it all correctly, since I really do not want to double up all site content because of this problem and would rather redirect it all to the one consistent protocol.
thanks in advance
Have your .htaccess like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.mysite.net%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} https [OR]
RewriteCond %{SERVER_PORT} ^443$
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mysite.net/.*$ [NC]
RewriteRule \.(gif|jpg|png|tif|js|css|xls|xlsx|zip|rar|pdf|ods|ots)$ - [F,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php/$1 [L]

URL forwarding with two arguments

This is my current .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?u=$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
It turns www.example.com into example.com AND interprets example.com/username AS example.com/index.php?u=username
Now I want to pass a second argument like example.com/index.php?u=username&e=email and still keep the format example.com/arg1&arg2. How do I do that in .htaccess?
First, you need to move the redirect before your routing rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Then, you want to check the 2 argument route:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA]
(this assumes a URL that looks like: http://example.com/username/emailaddress, but if you really want the & to separate them, use this rule instead:
RewriteRule ^([0-9a-zA-Z-]+)&([^/]+)/?$ /index.php?u=$1&e=$2 [L,QSA]
And this assumes a URL that looks like: http://example.com/username&emailaddress
Now your original rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ /index.php?u=$1 [L,QSA]