I have this simple .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !www.mydom.net [NC]
RewriteRule (.*) www.mydom.net/$1 [R=301]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
Works fine, except entering with query string mydom.net/?view=xyz; it then goes into a redirect loop:
https://mydom.net/var/www/domdir/www.mydom.net/var/www/domdir/www.mydom.net/var/www/domdir/...
but more strange is why is it inserting the DocumentRoot=/var/www/domdir in the first place?
What's wrong?
Added explanation for the record: this was intended to force all requests to mydom.net to be https://www.mydom.net with or without query string.
Why a query string would make a difference sounds like a non-related browser cashing issue from your 301 permanent redirect.
Let's say they entered http://mydom.net/?view=xyz
The first rule doesn't begin with http or https so it treats your rewrite like a directory. You'd be rewritten to http://mydom.net/www.mydom.net then, because your first rule has no L flag your 2nd rule applies, so you get rewritten to: https://mydom.net/www.mydom.net the .htaccess file is read from the top since the URL changed. no www. detected, so you get rewritten to http://mydom.net/var/www/domdir/www.mydom.net/ etc.
I believe a fix would be something close to:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.mydom.net [NC]
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^ https://www.mydom.net/%1 [R=301,L]
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} ^/?(.*)$
RewriteRule ^ https://%{HTTP_HOST}/%1 [R,L]
Related
I need to make a 301 redirect from
https://www.example.net/sub1/specific_keyword/page1/page2
https://www.example.net/sub2/sub3/specific_keyword/page3/page4
https://www.example.net/specific_keyword/page5/page6
to
https://www.example.net/sub1/
https://www.example.net/sub2/sub3/
https://www.example.net/
I tried this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.net$ [NC]
RewriteRule ^(.*)\/specific_keyword$ "https\:\/\/www\.example\.net\/$1" [R=301,L]
But no luck.
With your RewriteRule attempt, you demanded that the requested URL
ends with /folder-to-remove, via the $ at the end, so that won’t match (Source: #comment120998408_68464303)
With that fixed, place following rules at top of your htaccess Rules file. Make sure to clear your browser cache before testing your URLs or use a redirect checker online.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule ^([^/]*)/([^/]*)/.*$ $1/$2? [R=301,NE,L]
OR only match the specific keyword
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)specific_keyword/ https://www.example.com/$1 [R=301,L,NE]
My current .htaccess looks like this:
Redirect 301 /~mysite-net/vlog http://www.example.net/vlog
Redirect 301 /~mysite-net/pp https://www.example.net/pp
Redirect 301 /~mysite-net/pp02 https://www.example.net/pp02
Redirect 301 /~mysite-net/pp03 https://www.example.net/pp03
Redirect 301 /~mysite-net/ http://www.example.net/
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+).html$ https://www.example.net/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*?)/?$ index.php?s=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ index\.php\?s=([^\s]*)
RewriteRule ^/?(.*?)/?$ %1?%2%3 [L,R=301]
this does what intended so no problem. However, now I need to modify it so it always redirects to the SSL version of my site. It means no matter if the user types http it will always redirect them to https instead.
So I just added these lines at the bottom:
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]
but it's not working. What am I missing?
May be this can be work
I think you forgot to put RewriteCond %{HTTPS} !on
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
So I just added these lines at the bottom:
RewriteCond %{HTTP_HOST} ^example\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.net/$1 [R,L]
There are two problems with your existing directives:
You have put them in the wrong place in your file - they need to go at the top, not the bottom. By placing them at the end of the file they will only get processed when requesting static resources, because the preceding directives will otherwise "catch" the URL.
You are trying to combine the non-www to www redirect in this directive as well, however, you have omitted the OR flag on the first condition, so it will never redirect requests for http://www.example.net/foo.
In other words, at the top of your file.
RewriteCond %{HTTP_HOST} ^example\.net [NC,OR]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://www.example.net/$1 [R=301,L]
The RewriteCond %{HTTPS} !on condition mentioned in the other answer is simply an alternative to your SERVER_PORT condition.
I have the following code:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^xyz xyz.php [PT]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
First redirect redirects www.example.com/xyz to www.example.com/xyz.php.
This works fine.
Second one has a problem.
If I type in example.com, it creates an infinite loop to:
https://example.com/example.com/example.com
If I type in www.example.com, it also creates an infinite loop similar to the above.
I want to redirect everything from:
http://example.com/...
https://example.com/...
to
https://www.example.com/...
What am I missing here?
Looks like I didn't have RewriteCond %{SERVER_PORT} 80 declared before the redirect.
Allright so I have a website where I want traffic to go from
http -> https
non www -> www
So the final url should look like: https://www.x.net
I set up some rewrite rules in my htaccess, which look like the following:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
It works fine, everything gets redirected but the only problem is the following type of urls.
x.com/help
What happens is it will redirect to
https://www.x.comhelp
It removes the slash between com and help, I couldn't find an answer on Google so that's why I though, maybe my StackOverflow friend could help me out :)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
You should not use both the %{REQUEST_URI} server variable and the $1 backreference together. Use one or the other, see below...
You would need to change your directives to the following. This uses a backreference to the RewriteRule pattern which notably excludes the slash, so this must be included in the substitution:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=302,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=302,L]
Alternatively, use the %{REQUEST_URI} server variable instead. (This includes the slash prefix.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
Change the 302 (temporary) redirect to a 301 (permanent) redirect when you are sure it's working OK. (301 redirects are cached by the browser, so make sure your caches are cleared before testing.)
I have two domain names for the same website, and need to do a correct rewrite, so that whenever someone accesses the first domain name and all subdirectories, with, or without www. they get redirected to the second domain and subdirectories without www.
I managed to set the redirect for the domain name without subdirectories, but for whatever reason, subdirectories are not getting rewritten.
So when I go to domainnamenumberone.com, or www.domainnamenumberone.com, i get redirected to domaintwo.com – however, when I go to domainnamenumberone.com/wordpress/path or www.domainnamenumberone.com/wordpress/path I remain there, and nothing gets rewritten.
Here's what I placed in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
Would be grateful for your help!
You need to place this rule as very first rule in DocumentRoot/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domaintwo\.com [NC,OR]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainnumberone\.com [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(\S*)\s [NC]
RewriteRule ^ http://domaintwo.com/%1 [L,R=302,NE]
Then add this line in each child .htaccess like wordpress/ or wordpress/path/ (wherever .htaccess already exists) below RewriteEngine On line
RewriteOptions Inherit
You can use that:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^domainnumberone\.com [NC]
RewriteRule ^(.*)$ http://domaintwo.com/$1 [L,R=301]
But it seems to me that it should also work with yours.....
try a different browser (cache problem)