I am trying to redirect to a new domain, but it isn't retaining the complete url on the redirect, it only goes to the homepage. Here is the line in my htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*\.old-domain\.com
RewriteRule ^(.*)$ http://www.new-domain.com/$1 [R=permanent,L]
For example, if I go to www.old-domain.com/contact-us/request-a-demo/, it should go to www.new-domain.com/contact-us/request-a-demo/
Instead, anything I type after the first "/" is redirecting to the homepage for www.new-domain.com
What am I missing, please help
Maybe you could try this:
RewriteEngine On
RewriteCond %{SERVER_NAME} ^(.+\.)?old-domain\.com
RewriteRule ^/?(.*)$ http://www.new-domain.com/$1 [R=permanent,L]
Related
I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule
Need some help trying to redirect a domain:
https://www.example.com/blog/page/10/?page_id=%2Ffeed%2Fatom%2F to /blog/
I think the best way is to just redirect anything after /page
The code below is what I'm thinking but I know is not correct:
RewriteEngine on
RewriteCond %{THE_REQUEST} /blog/page/[0-255]* [NC]
RewriteRule ^ /blog/? [NC,R=301,L]
Any advice?
Need some help trying to redirect a domain:
If you are trying to redirect the domain use something like
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteRule ^(.*)$ "http://www\.exampe2\.com\/blog/" [R=301,L]
If you want to append the request to the URL use $1
If you're rewriting the path to /blog/ and want to keep the query string use the [QSA] flag ie.
RewriteRule /blog/(.*) /blog/ [R=301,QSA,L]
this will make sure you keep your query string.
I have been providing my subdomain name to friend until he manages to get new domain.
Now he has it and I would like to 301 redirect all the old links to his domain:
Example:
http://subdomain.my-domain.com/post.php?118&tg=602643
Redirect to
http://subdomain.com/post.php?118&tg=602643
So I want to keep up all the variables behind the post.php in the redirect
I am .htaccess newbie - can you please help me with providing the correct Rewrite rule?
Also, If you happen to have any good article about .htaccess and how to manage it, link is really appreciated.
Thanks
EDIT
Here are actual usecases I want to do:
redirect
http://raketa2.tasselhof.com/nastenka.php?115&up=648483
to
http://www.raketa2.cz/nastenka.php?115&up=648483
However, since there is no more subdomain existent on my site, i see the 404 error on my main page as this:
/nastenka.php?115&up=648483 -> Provided 404 Error
I tried:
RewriteCond %{Request_URI} ^/nastenka\.php [NC]
RewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://www.raketa2.cz/$1?%{QUERY_STRING} [R=301,L]
But with no good...
SOLVED
Duh! I am really dumb. I just added two new CNAME records to my domain DNS. Should do the trick
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{HTTP_HOST} ^subdomain\.
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
The QUERY_STRING post.php?118&tg=602643 will be present.
Assuming that there is no post.php on your domain,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
If post.php is present on your domain but, you do not use similar query strings,
Do this:
RewrtiteEngine on
ReWriteBase /
RewriteCond %{Request_URI} ^/post\.php [NC]
RFewriteCond %{QUERY_STRING} ^\d+&tg=\d+$ [NC]
RewriteRule ^(.*)$ http://subdomain.com/$1?%{QUERY_STRING} [R=301,L]
I want htp://www.seostuff.org.ua/?s=seo to be redirected to htp://seo.seostuff.org.ua
Instead of seo can be any other search pattern
Buy this way I managed to make htp://seo.seostuff.org.ua redirecting to htp://www.seostuff.org.ua/?s=seo
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.seostuff\.org\.ua
RewriteCond %{HTTP_HOST} ^(.*)\.seostuff\.org\.ua [NC]
RewriteRule .* http://www.seostuff\.org\.ua/?s=%1 [L]
But I do not want URL change it should stay the same, ex htp://seo.seostuff.org.ua
I want revert requests to be processed as well (means htp://www.seostuff.org.ua/?s=seo should 301 redirect to htp://seo.seostuff.org.ua)
Also I do not need any slowness of processing such URL requests. Want to create optimized Rules. Any help please?
I would really appreciate this.
You cannot rewrite to a full HTTP URL without redirect to it.
So try this:
RewriteEngine on
# rewrite abc.seostuff.org.ua to abc.seostuff.org.ua/?s=abc
RewriteCond %{HTTP_HOST} !^www.seostuff.org.ua
RewriteCond %{HTTP_HOST} ^(.*).seostuff.org.ua
RewriteRule .* ?s=%1 [L]
# redirect seostuff.org.ua/?s=abc to abc.seostuff.org.ua
RewriteCond %{QUERY_STRING} ^s=([a-zA-Z0-9\-_]+)$
RewriteRule .* http://%1.seostuff.org.ua/ [L,R=301]
I want to :
- switch from http to https if http is used
- redirect the subdomain to index?o=subdomain except www
- redirection the subdirectory to index?u=user
Example :
http://www.mydomain.com will be redirected to https://www.mydomain.com
http://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
https://subdomain.mydomain.com will be redirected to https://www.mydomain.com/index?o=subdomain
http://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
https://subdomain.mydomain.com/user will be redirected to https://www.mydomain.com/index?o=subdomain&u=user
Is mod_Rewrite the best to do that ? Any idea ?
Thanks in advance
I don't have time to test it right now, but you can try this and see if it works. There may be some potential for some things to go wrong, so if you have trouble with it I'd be happy to work out any kinks later. Also, I think that I covered everything you wanted to do, but let me know if I left something out.
RewriteEngine On
# Force redirect to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}/$0 [R=301,L]
Edit: I've updated the ruleset below. I thought about your question though, and aren't you going to have issues attempting to serve up your subdomains over TLS/SSL? That aside, one of the following should do what you want (without errors this time, I hope):
If you wanted internal redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ /index?o=%1%2
If you wanted external redirection:
RewriteCond %{HTTP_HOST} !=mydomain.com
RewriteCond %{HTTP_HOST} !^www
RewriteCond %{REQUEST_URI} !^/index
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^([^\.]+)[^/]*/([^/]+)?
RewriteCond %1&u=%2 ^([^&]+)(&u=.+)?
RewriteRule ^.*$ https://www.mydomain.com/index?o=%1%2 [R=301,L]