I am trying to solve a problem with my .htaccess file.
I would like to use 301 redirect from my "old" doman to the "new" one, except some certain pages. Like "PageA" and "PageB".
The code thats working and iam using for redirect all is this:
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.old.com [NC]
RewriteRule ^(.*)$ http://www.new.com/$1 [L,R=301,NC]
"PageA", and "PageB" has to be redirected to the "new" domain too, but to a certain address. Like:
old.com/pageA ---> new.com/something/pageA
old.com/pageB ---> new.com/something2/pageB
What code should i add to .htaccess, to add some exceptions with certain addresses ?
Try below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(pageA|pageB)$
RewriteRule ^ http://www.new.com/test/%1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^old.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com [NC]
RewriteRule ^ http://www.new.com/$1 [L,R=301,NC]
Related
I just want some help with my .htaccess configuration. Before I have this problem where I need to redirect the page to
a specific url. Like if the user type http://mydomain/test it will be redirected to http://mydomain/app/r/test. I
achieved the redirection using .htaccess. The code is below
RewriteEngine on
RewriteRule (.*) http://mydomain/app/r/$1 [R=301,L]
But I have this new specification. As follows:
If user visit http://mydomain/ or http://domain/index.php, he will be
redirected to http://mydomain/mymanager
If user visit
http://mydomain/{any_segment}, he will be redirected to
http://mydomain/app/r/{any_segment}
Here is my current htaccess. Basically in the url, any characters will be redirected to /app/r/{} except index.php. But what about the http://domain/? How can I achieve this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
I've been playing around my htaccess and any pointers will be of great help to me.
These are your rules :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain$ [OR]
RewriteCond %{HTTP_HOST} ^www.mydomain$
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
It looks like , any request should redirect to /app/r/ except A URI start with /index.php but still not doing that , in normal cases , so you should exclude the target directory from being redirect again and again by this rule RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L] , ^(.*)$ means any URI start with any thing and the server will look, if there is a condition , and actually there is only one condition to exclude index.php, otherwise redirect .
So , you should exclude this target directory along with index.php like this :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/)
But still you want to match a request that target HOST only so,you should add that as well :
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
Then you could match host with www or non-www like this :
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
Your rules should look like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteCond %{REQUEST_URI} !^(/index\.php|/app/r/|/?$)
RewriteRule ^(.*)$ http://mydomain/app/r/$1 [R=301,L]
Also you could summarize that more by utilizing %{REQUEST_URI} like this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain$
RewriteRule !^(index\.php|app/r/|/?$) http://mydomain/app/r%{REQUEST_URI} [R=301,L]
NOTE: Clear browser cache then test
I am having an issue with subdomain.. i have a subdomain content.domain.com
which i want to use for cookieless domain content..
but when i wrote it in website.. its automatically add full website url.. which i have added in .htaccess
can you please let me know how can i stop redirecting the subdomain ....
Please help......
here is code for htaccess for domain redicretion
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
You can combine both rules into a single rule
Using a RewriteCond you can omit a subdomain.
Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?!content\.domain\.com)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your browser cache while testing this change.
I don't have much experience with .htaccess rewrite rules so I'm hoping that someone can help.
I've created a subdomain for a website that I want to use for development work however I think that the current .htaccess file is causing a redirect to the main domain. This is the current .htaccess file:
<Files 403.shtml>
order allow,deny
allow from all
</Files>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F,L]
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^160\.153\.16\.12
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
The sub domain is dev.website.co.uk
Any advice will be very gratefully received.
Your .htaccess has rules which tell it to redirect to the main domain. Just remove them from your subdomain's .htaccess.
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^160\.153\.16\.12
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
# I don't know what these are for
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
I see many problems in your .htaccess, may be you have posted only part of it.
Main problem I see, the following lines catch any case where HTTP_HOST is different from www.domain.co.uk and redirect to www.domain.co.uk
RewriteCond %{HTTP_HOST} !^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/$1 [L,R=301]
The following catch any case HTTP_HOST is different from ^160\.153\.16\.12 and redirect to www.domain.co.uk
RewriteCond %{HTTP_HOST} ^160\.153\.16\.12
RewriteRule (.*) http://www.domain.co.uk/$1 [R=301,L]
As far as I know, the remaining RewriteCond are useless without a corresponding rewrite rule.
RewriteCond %{HTTP_HOST} ^domain\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.co\.uk$
The RewriteCond directive defines a rule condition. One or more
RewriteCond can precede a RewriteRule directive. The following rule is
then only used if both the current state of the URI matches its
pattern, and if these conditions are met.
And at end, as #Marc B stated, you need to explain where the .htaccess is placed and if both the sites share the same root.
Currently I am redirecting all URLs to my index.html files:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html$
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule .* /index.html [L,R=302]
I want to exclude two URLs, for example:
www.mydomain.com/i-am-a-domain
www.mydomain.com/super-cool
I found several snippets to exclude URLs, but I was not able to include them, because they use a different setup (at least I think so).
As RewriteCond sets conditions, i guess I have to add a line here. I have tried adding:
RewriteCond %{REQUEST_URI} !/(i-am-a-domain|super-cool)\ [NC]
Which does not work. How can I achieve that?
Change your existing condition as this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/(index\.html|i-am-a-domain|super-cool)/?$ [NC]
RewriteCond %{REQUEST_URI} !\.(gif|jpe?g|png|css|js)$
RewriteRule ^ /index.html [L,R=302]
However if you have other rules also in this .htaccess then I suggest using THE_REQUEST variable which doesn't get overwritten after execution of some internal rewrite rules.
RewriteCond %{THE_REQUEST} !\s/+(index\.html|i-am-a-domain|super-cool)[?\s/] [NC]
RewriteCond %{THE_REQUEST} !\.(gif|jpe?g|png|css|js)\sHTTP
RewriteRule ^ /index.html [L,R=302]
I want to have dynamic subdomains, such as:
(.*).mydomain.com
that I can retreive with $_GET['subdomain']. I have added an wildcard DNS record (* A xx.xx.xx.xx), and now for every subdomain I go to (e.g. test.mydomain.com) all I get is:
Apache is functioning normally
I have tried this code in .htaccess, but nothing seems to help:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/?subdomain=%1 [L]
Thanks in advance!
Are you looking for something more like this?
RewriteCond %{QUERY_STRING} !subdomain=
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ /$1?subdomain=%1 [L,QSA]