Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.
Related
the following rules works on http but don't on https.
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1? [L]
RewriteRule ^/path/file.html$ https://www.domain.tld/path/file/ [R=301,L]
Why the query_string part doesn't work in https?
Based on the comments of your question, it appears that you just want to remove the query string anytime there is a page parameter. This type of rewrite rule doesn't strip off or change the URL unless there is a redirect. So if you don't add R=301 or R to the rewrite rule, the query string will not be removed. All of the following worked on my server to remove the query string, and my server is 100% HTTPS:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1? [R=301,L]
Or you may be able to use the QSD flag instead of question mark:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1 [R=301,L,QSD]
Or you may be able to use something like this:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule .* /? [R=301,L]
Or just R (for 302) instead of R=301:
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1? [R,L]
But in no case was the query string removed unless I used a redirect.
Really all you need is QSD http://httpd.apache.org/docs/current/rewrite/flags.htm
RewriteCond %{QUERY_STRING} "page=" [NC]
RewriteRule (.*) /$1 [L,QSD]
I'm not sure why it would work on http but not https unless you're using separate vhosts for http and https and the settigns were slightly different
Remember that Rewrite rules are internal unless using the R flag. When you use the R flag it tells the browser to go to a different page causing a full server/client round-trip. Otherwise, it just changes the request and proceeds as normal.
I'm not really strong with .htaccess file, and I can't explain what's going on behind the scenes, but from my opinion your .htaccess file should look like this:
RewriteCond %{QUERY_STRING} page= [NC]
RewriteRule (.*) /$1? [L]
RewriteRule ^path/file.html$ https://www.domain.tld/path/file/ [R=301,L]
It work perfectly for
http://example.com?page=1
https://example.com/?page=1
http://example.com/path/file.html
https://example.com/path/file.html
Tested (with love) here
I am having some issues with URL re-writing that I cannot figure out. Here's the problem.
This URL IS NOT redirecting properly:
http://domain.com/index.php?en=oldpage
HOWEVER, this URL IS redirecting properly:
http://www.domain.com/index.php?en=oldpage
The only difference in the url that is not redirecting properly is the absence of the www.
Here is the re-write I am using:
RewriteCond %{QUERY_STRING} ^en=oldpage
RewriteRule ^(index.php/|)$ /newpage.html? [R=301,L]
I also have this re-write BEFORE other re-writes, to handle url's without "www.":
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Why will it not re-direct without the "www." ? Any help would be much appreciated. Thank you.
I guess there is some code/rule below those rules adding query string back to your URLs.
Try these rules instead based on THE_REQUEST variable which doesn't get overwritten:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?en=oldpage [NC]
RewriteRule ^ /newpage.html? [R=301,L]
Make sure to test it after clearing your browser cache.
I've been trying to figure out how to write a new rewrite rule but I am struggling badly. We are removing the world-wide (WW) site from domain.com and want to forward any old links to the country selector. Any help would be appreciated. Thank you in advance.
# if any user goes to this URL
www.domain.com/products/folder1/folder2/index?sku=123&var2=abc&isocountrycode=ww
# forward them to this URL
www.domain.com/choose/country_selection?ref_url=/products/folder1/folder2/index.cfm?sku=123&var2=abc
# my attempt that does not work
RewriteCond %{QUERY_STRING} ^(.*)isocountrycode=ww(.*)$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=$1?%1%2 [L,R=302]
I have been using this website to test my code http://martinmelin.se/rewrite-rule-tester/
Answered my own question. Helicontech APE requires the second ? to be escaped with a backslash...
RewriteCond %{QUERY_STRING} ^(.+&)?isocountrycode=ww&(.+)?$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=/$1\?%1%2? [NC,R=301,L]
RewriteCond %{QUERY_STRING} ^((.+)&)?isocountrycode=ww$ [NC]
RewriteRule ^(.+)$ /choose/country_selection.cfm?ref_url=/$1\?%2? [NC,R=301,L]
I am trying to make a redirect for a subdomain but i can't figure it out.
This what I have now:
RewriteCond %{HTTP_HOST} ^(mysubdomain).myhost.com$ [NC]
RewriteRule (.*) http://www.myhost.com/folder/index.html [R=301,L]
But when I go to the subdomain address in my browser, there's no redirect.
I need this for only one specific subdomain.
Where am I going wrong?
Should be:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^.*$ http://www.domain.com/folder/ [R=301,L]
Try this, since you only need it from one domain. The () are pointless in both cases. Especially on the 2nd line where you aren't passing the old URL to the new URL.
RewriteCond %{HTTP_HOST} ^www.old-domain.com$ [NC]
RewriteRule ^.*$ http://www.myhost.com/folder/index.html [R=301,L]
I have a subdomain setup as onlinedev.domain.com
I need to use htaccess to rewrite to domain.com/online_content, while still showing onlinedev.domain.com in the address bar (SSL is for onlinedev.domain.com).
this is what I currently have that is very close:
php_flag display_errors off
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST}<->%{REQUEST_URI} ^(www\.)?([^.]+).*<->/([^/]+) [NC]
RewriteCond %2<->%3 !^(.*)<->\1$ [NC]
RewriteRule ^(.+) /%2/$1 [L]
This correctly rewrites to domain.com/onlinedev, but if I try to change the RewriteRule to:
RewriteRule ^(.+) /online_content/$1 [L]
I get an error
I understand that there are typically better ways to do this subdomain work, but without getting into server config and DNS details, I need to do it with htaccess.
And yes, I do need to rewrite to a directory that has a different name than the subdomain.
Well, I figured it out.
The issue was that I was causing an infinite loop.
Once the rewrite had happened, it was still trying to rewrite to the directory.
Here is my new htaccess that took care of it:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} onlinedev\\.domain\\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/online_content/
RewriteRule ^(.+) /online_content/$1 [L]
Notice that I added a check to make sure that the REQUEST_URI is not the name of the directory I am rewriting to.
Try this rule:
RewriteCond %{HTTP_HOST} =onlinedev.example.com [NC]
RewriteCond $0 !^online_content/
RewriteRule .+ /online_content/$0 [L]