This code affect my subdomains as well.
Redirect 301 /some-adress /
For example "subdomain1.domain.com/some-adress" will redirect to "subdomain.domain.com/"
I only wanted to redirect "domain.com/some-adress" to "domain.com/". Not the subdomains. How can I prevent it from redirecting the subdomain?
You can convert that rule into mod_rewrite with a condition to allow this rule for main domain only.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^some-adress / [L,R=301]
Related
My aim is to redirect my current old domain to my new domain. All the page structures are the very same, only thing that has changed is the domain.
I'm wanting to redirect the entire site to the new domain within my .htaccess file. What exact line of code will I use?
Are you using mod_alias Apache module?
Redirect 301 / http://www.new-domain.com/
Use the mod_rewrite Apache module
RewriteEngine On
RewriteRule ^(.*)$ http://new_domain.com/ [R=301]
Maybe:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^old_domain\.com$ [NC]
RewriteRule ^(.*)$ http://new_domain.com [R=301,L]
This is a good tutorial about your problem...
I need to redirect WWW and non-WWW domain to different domain ending. Both domains are on the same cPanel.
How do I do that via .htaccess? This is my current .htaccess:
RewriteCond %{HTTP_HOST} ^abc\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.abc\.com$
RewriteRule ^/?$ "http\:\/\/www\.abc\.co\.nz\/" [R=301,L]
It depends what you want to redirect. But for example if you wanted to redirect a page called abc to a website: def.com you would use:
redirect 301 /abc http://def.com
Not sure if this is exactly what you want but I hope it helps.
In that case then all you need to do is put this code in your .htaccess of domain.com:
redirect 301 / http://domain.co.nz
This will redirect any part of domain.com to domain.co.nz
I would like to do a simple 301 direct on some old URLs that have parameters to them.
e.g.
redirect 301 /display.php?id=2 http://www.url.com.au/about/
redirect 301 /display.php?id=4 http://www.url.com.au/news/
My experience with .htaccess is limited. What would be the simplest and the most correct way to enable the above redirects to actually work.
This should work given that you don't have other rules that may interfere with this one:
RewriteCond %{QUERY_STRING} ^id=2$
RewriteRule ^display.php$ /about/ [R=301,L]
RewriteCond %{QUERY_STRING} ^id=4$
RewriteRule ^display.php$ /news/ [R=301,L]
If the domain is not the same domain then you may include the full qualified domain as in:
RewriteCond %{QUERY_STRING} ^id=2$
RewriteRule ^display.php$ http://www.url.com.au/about/ [R=301,L]
I have a list of 301 Redirects like this inside htaccess:
Redirect 301 /oldpage1.php http://www.domain.com/newpage1.php
Redirect 301 /oldpage2.php http://www.domain.com/newpage2.php
Redirect 301 /oldpage3.php http://www.domain.com/newpage3.php
...
Now these pages shall be redirected only for a certain domain (there are other domains now pointing to the same location like www.domain.com).
E.g. www.domain.com/oldpage1.php shall be redirected, but www.sub.domain.com/oldpage1.php not.
So whats the way to apply these redirects only for www.domain.com?
Thanks.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage1\.php/?$ newpage1.php [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage2\.php/?$ newpage2.php [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^oldpage3\.php/?$ newpage3.php [L,R=301]
PS: If you have several rules like above then it is much better to use RewriteMap.
I want write a rule in .htaccess for redirecting
http://www.dir.domain.com
http://dir.domain.com
http://www.domain.com/dir
these should redirect to
http://domain.com/dir
Thanks in advance...
The trick here is to use an .htaccess file in your DOCROOT
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?dir\.domain\.com$
RewriteRule ^.* http://domain.com/dir/$0 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^dir/.* http://domain.com/$0 [R=301,L]
Note that this is doing a 301 redirect -- that is the server tells the browser that this is a permanent redirect and the browser caches this redirect.
You can also do an internal redirect where this mapping is done silently at the server end by tweaking flags.
This assumes that your shared service is using Apache, if its using IIS, then you need to do something similar with the web.config
You can try:
RewriteCond %{HTTP_HOST} ^((?!www).+)\.domain\.com$
RewriteRule ^ http://domain.com/%1 [L,NE]
Please change domain by your domain, when you enter sub1.domain.com then it will redirect to domain.com/sub1, when you enter sub2.domain.com then it will redirect to domain.com/sub2 ....