Following are the redirection rule I have in my htaccess file. They redirect https://olddomain.com to https://subdomain.domain.com
but the web pages are not getting redirected. I still have olddomain.com/page1 loading.
`RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]`
I added the following rule which is working partially, the slash after the domain is missing. Now the redirect is https://subdomain.domain.compage1 instead of
https://subdomain.domain.com/page1
RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]
How to fix this. Any help please. (I tried the redirect without escaping \ at the end but this didn't work.
RewriteRule ^ https\:\/\/subdomain\.domain\.com{REQUEST_URI} [L,R=301])
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule ^/?$ "https\:\/\/subdomain\.domain\.com\/" [R=301,L]
These redirect the homepage (root directory) only. To redirect any URL-path to the same URL-path at the target use the following instead:
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule ^ https://subdomain.domain.com%{REQUEST_URI} [R=301,L]
I've combined your two conditions.
Note that the syntax is %{REQUEST_URI} (with a % prefix) to reference the REQUEST_URI server variable. "{REQUEST_URI}" is otherwise just literal text.
There is no need to backslash-escape all those characters in the substition string.
Clear your browser cache before testing. And preferably test with 302 (temporary) redirects to avoid potential caching issues.
I added the following rule which is working partially, the slash after
the domain is missing. Now the redirect is
https://subdomain.domain.compage1 instead of
https://subdomain.domain.com/page1
RewriteRule ^ https\:\/\/subdomain\.domain\.com\/{REQUEST_URI} [L,R=301]
Except that that is not possible with the directive as stated - you were perhaps seeing a cached response as a result of earlier erroneous redirects. 301 (permanent) redirects are cache persistently by the browser. Always test with 302 (temporary) redirects and ideally with the browser cache disabled (option on the "Network" tab) of the browser dev tools.
Alternative solution
This would need to go in the root .htaccess file (it would not work if it is in a subdirectory .htaccess, unlike the rule above).
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com [NC]
RewriteRule (.*) https://subdomain.domain.com/$1 [R=301,L]
This uses a backreference instead of the REQUEST_URI server variable.
This redirect needs to go near the top of the .htaccess file, before most other directives.
Make sure you've cleared your browser cache before testing.
Related
We have installed a SSL for our site and I have created an .htaccess with the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# mobile redirect
RewriteCond %{HTTP_HOST} ^www\mobile.example\.com [NC]
RewriteCond %
{HTTP_USER_AGENT}"android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule ^(.*)$ https://mobile.example.com/$1 [L,R=302]
</IfModule>
This code works great coming from the desktop, but the mobile part is not working. What am I missing?
...but the mobile part is not working.
You've not stated explicitly what the "mobile" part is expected to do. However, the "mobile part" in your code would seem to just be a www to non-www redirect. The HTTP to HTTPS redirect is separate to this and does not differentiate between mobile and desktop (and neither would it necessarily need to).
However, there are several issues with the directives in the "mobile part" that will prevent it from "working" (and also with the HTTP to HTTPS redirect).
The directives are in the wrong order. Both of the external redirects (HTTP to HTTPS and "mobile" www to non-www) should be before the internal rewrite (the first couple of rules)
I assume ENV:HTTPS (that references an environment variable called HTTPS) is as per instruction from your webhost. This is non-standard, although not uncommon with some shared hosts.
RewriteCond %{HTTP_HOST} ^www\mobile.example\.com [NC] - You are missing a dot after the www subdomain (assuming that is what you trying to match). So, this will never match. You are also missing a slash before the dot in the middle of the regex (to match a literal dot, not any character). The CondPattern should presumably read ^www\.mobile\.example\.com in order to match the www subdomain.
RewriteCond % {HTTP_USER_AGENT}"android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC] - You are missing a space after the first argument %{HTTP_USER_AGENT}<here>. Although you also appear to have an erroneous space after the %. Either way, this will fail to match. However, I would also question why you specifically need to match the mobile user-agent here? I would think you need to redirect www to non-www regardless of user-agent? Why would you permit a desktop user-agent access to www.mobile.example.com? So, this condition can perhaps be removed entirely.
Not a bug, but you probably don't need the <IfModule> wrapper, unless these directives are optional and you are porting the same code to multiple servers where mod_rewrite might not be available. See my answer to a related question on the Webmasters stack: https://webmasters.stackexchange.com/questions/112600/is-checking-for-mod-write-really-necessary
Again, not a bug, but the RewriteBase / directive in this block of code is entirely redundant.
Taking the above points into consideration, it should be written more like this:
RewriteEngine On
# HTTP to HTTPS redirect - all hosts
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
# mobile redirect
RewriteCond %{HTTP_HOST} ^www\.mobile\.example\.com [NC]
RewriteCond %{HTTP_USER_AGENT} "android|blackberry|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
RewriteRule (.*) https://mobile.example.com/$1 [R=302,L]
# Front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Like I said in the notes above, I question the use of the RewriteCond %{HTTP_USER_AGENT} directive to detect mobile-only user-agents. If all users should be redirected www to non-www (as it looks like they should) then simply remove this condition. This should also presumably be a 301 (permanent) redirect once you have confirmed that it works as intended.
Taking this a step further, don't you also want to canonicalise desktop clients as well? ie. Redirect www to non-www on all hosts?
This code works great coming from the desktop
Although there's no reason why this didn't work "great" from mobile either if you were requesting the conanical host, ie. https://mobile.example.com/.
UPDATE: What I need for the .htaccess to do is redirect all traffic - desktop and mobile etc - to the new https instead of HTTP.
By the sounds of it you only need a "simple" HTTP to HTTPS redirect. The "front-controller pattern" that you have seemingly copied from the webhost's article may be in error?
Try the following instead in the root .htaccess file.
RewriteEngine On
# Redirect all requests from HTTP to HTTPS on the same host
RewriteCond %{ENV:HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You should remove all other directives and make sure there are no other .htaccess files in subdirectories.
The REQUEST_URI server variable contains the requested URL-path. This will be required, instead of using a backreference as you had initially, if your mobile subdomain points to a subdirectory off the main domain's document root (which you hint at in comments, but not stated in the question).
You must clear the browser cache before testing and test first with 302 (temporary) redirects before changing to a 301 (permanent) redirect only once you have confirmed the redirect works as intended.
I have an site lets say https://example.com/ and I would like to redirect every url that doesn't begins with /something to https://example.com/something/
I'm using Apache 2.4.29 (hosting) and my .htaccess looks like this.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/something(.*)$ [NC]
RewriteRule ^(.*)$ /something/ [R=302,NC,L]
My problem is that when I'm on homepage (/) or any other page I'm redirected to /something/ which is correct but when I'm on /something/ I'm still beeing redirected to /something/ and it loops until ERR_TOO_MANY_REDIRECTS error shows up.
Here is a link to htaccess tester which shows that this should work and should not redirect me to /something/ when I'm already here but it is not the case on my hosting.
I was following this and this question but without success.
With your shown attempts, please try following set of rules. Please place them on top of htaccess rules file in case you already have existing rules.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{THE_REQUEST} !something [NC]
RewriteRule ^ /something? [R=302,NC,L]
You can try this rule with THE_REQUEST variable:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/something [NC]
RewriteRule ^ /something/ [R=302,L]
Make sure to test it after completely clearing browser cache.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1
I have working code in .htaccess that accomplishes the following.
redirect non-www to www (and https:)
redirect http to https (and www)
allow accessing URLs ending with /file1.htm etc at /file
However, as I discovered, the third rule was more of a hack that needed to be used in combination with removing all existing links to the .htm versions (which is outside your control). As a result, Google started crawling both versions and deciding which ones are canonical.
I want to modify the third rule to not just allowing access at /file, but rewriting the URL with a 301 message. There are several answers on Stackoverflow that have worked for others that are not working for me due to existing rules in the file, so I'm posting it as a new question.
How do I add a 301 redirect to all /file.htm links to /file without breaking the 3 existing rules?
RewriteEngine On
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.htm [NC,L]
Redirect 301 /output.php /
I want to modify the third rule to not just allowing access at /file, but rewriting the URL with a 301 message.
Using a 301 there makes little sense – you want to keep this one an internal redirect.
You should add a new rule, that rewrites requests for /file.htm to /file externally, and have that one use a 301 status code:
#1) externally redirect "/file.htm" to "/file"
RewriteCond %{THE_REQUEST} /([^.]+)\.htm [NC]
RewriteRule ^ /%1 [NC,L,R=301]
#2) Internally map "/file" back to "/file.htm"
RewriteCond %{REQUEST_FILENAME}.htm -f
RewriteRule ^(.*?)/?$ /$1.htm [NC,L]
Need to redirect example.com/page to www.example.com/page_2. Can also be from www.example.com/page to www.example.com/page_2. I have tried
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/page [NC]
RewriteRule (.*) https://www.example.com/page_2 [R=301,L]
But that does not appear to work.
The HTTP_HOST server variable contains just the Host header, as its name suggests (eg. www.example.com), it does not include the URL-path as well (ie. /page).
You match the URL-path with the RewriteRule pattern. So, try the following instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^page$ https://www.example.com/page_2 [R=302,L]
Change the 302 (temporary) redirect to 301 (permanent) only when you are sure it's working OK (if this is indeed intended to be permanent). 301s are cached hard by the browser so can make testing problematic.
Unless you have multiple domains on this account, you can remove the RewriteCond directive altogether.
i'm going to redirect from
www.example.com/forum or example.com/forum
to
www.example.ir/forum
using htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.ir/forum$ [OR]
RewriteCond %{HTTP_HOST} ^example.com/forum$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com/forum$
RewriteRule (.*) http://www.example.ir/forum$1 [R=301,L]
i fail to do that and i always got:
www.example.irforum (without slash /) which is dead link.
The code you have posted could not possibly redirect to www.example.irforum (without slash). In fact, the code you have posted won't do anything (since none of the conditions will match). If you are seeing a redirect then it's possibly a previously cached (erroneous) redirect.
Assuming example.com and example.ir are pointing to the same place then try the following in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^forum(.*) http://www.example.ir/forum$1 [R=302,L]
The above rules are processed as follows:
Does the URL-path (less the directory prefix) of the request match the RewriteRule pattern ^forum(.*)? If so, then continue...
Does the HTTP_HOST server variable match the CondPattern. ie. Is the Host header either www.example.com or example.com? If so, then continue...
Redirect the request to http://www.example.ir/forum$1 - this is the RewriteRule substitution. $1 refers to the captured group (first parenthesised subpattern) in the RewriteRule pattern - ie. whatever matches (.*).
If example.com and example.ir point to different servers then you don't need the RewriteCond directive.
Test with 302 (temporary) redirects until you are sure it's working OK then change to 301 (permanent). Or, test with browser caching disabled.
The HTTP_HOST variable contains just the HTTP Host header. eg. example.com. This contains no path information. Use the RewriteRule pattern to match the URL-path.