So My problem is that when visiting example.com/members I am redirected to www.example.commembers/ (notice that this is not a valid web address) instead of members.example.com (It only works correctly when there is the www). However, this redirection seems to happen with or without the following htaccess file. What to I need to add/change to stop this problem.
This is my htaccess file located in the members folder:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com$1 [L,R=301]
RedirectMatch 301 ^/members/(.*)$ http://members.example.com/$1
Then there is the wordpress htaccess file in the root directory:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Problem is in your first rule. Change your code to:
RewriteEngine On
RewriteBase /members/
RewriteCond %{HTTP_HOST} !^members\. [NC]
RewriteRule ^(.*)$ http://members.example.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Related
I'm using this piece of code to redirect all http to https in my .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Works great. But my only problem is if I access the root non http
http://example.com redirects to https://www.example.com/dev/
/dev is the directory where the main site is located.
my cpanel directory is like this
public_html/dev <---- https://www.example.com
How can I fix this issue. It should redirect to https version without the dev directory.
Hope someone could shed a light on this. thanks.
EDIT:
To make it more clear here's the directory structure
The contents of .htaccess in public_html is
public_html/.htaccess
rewritecond %{HTTP_HOST} ^example.com$ [NC,OR]
rewritecond %{HTTP_HOST} ^www.example.com$
rewritecond %{REQUEST_URI} !dev/
rewriterule (.*) /dev/$1 [L]
RewriteCond %{HTTP_HOST} ^oldurl\.com$ [OR] // if old url redirect to example.com
RewriteCond %{HTTP_HOST} ^www\.oldurl\.com$ // if old url redirect to example.com
RewriteRule ^/?$ "https\:\/\/www\.example\.com" [R=301,L]
here's the contents of .htaccess file in public_html/dev
public_html/dev/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Have it this way.
public_html/.htaccess
RewriteCond %{HTTP_HOST} ^(?:www\.)?oldurl\.com$ [NC]
RewriteRule ^/?$ https://www\.example\.com [R=301,L]
# enforce HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# First add a trailing slash if dev/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/dev/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
# forward everything to /dev directory
RewriteCond %{REQUEST_URI} !^/dev/ [NC]
RewriteRule .* dev/$0 [L]
public_html/dev/.htaccess:
RewriteEngine On
RewriteBase /dev/
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Make sure to clear your browser cache completely before testing this changge.
I'm trying to redirect my subdmain in this manner: anysubdomain.my_domain.com and its URLs must be redirected to root domain and its URLs respectively.
For example (1) if a user tries http://anysubdomain.my_domain.com it should redirect tohttps://www.my_domain.com.
And (2) if a user tries http://anysubdomain.my_domain.com/anyurl it should be redirected to https://www.my_domain.com/anyurl.
I was able to achieve (1) using below htaccess code:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
</IfModule>
yet I could not do the (2) part. I'm sure we can do this with a modification for above htaccess code, but I don't know how.
I already have wildcard cname, that is why (1) perfectly working.
Also it is a WordPress site, if that matters.
Put these rules first :
RewriteCond %{HTTP_HOST} !^www.my_domain.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
like this :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?my_domain\.com$
RewriteRule ^(.*)$ https://www.my_domain.com/$1 [QSA,L,R=301]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note: clear browser cache the test.
You may use this .htaccess to handle all subdomains:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} (?:^|\.)(my_domain\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
Make sure to test it from new browser to clear browser cache fully.
I'm trying to redirect a single WordPress page (/scheduling/) to HTTPS, while forcing HTTP on all of the other pages. There are a lot of similar posts on this topic, but my specific use case isn't working for some reason. Thoughts?
.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteCond %{REQUEST_URI} ^scheduling/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^scheduling/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The above is properly redirecting all other pages to be redirected to http whenever https is requested, but isn't doing anything at all for the scheduling/ page.
Do redirects before default WP rule.
Use THE_REQUEST variable instead of REQUEST_URI.
You can use:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} /scheduling [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/scheduling [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
How would I exclude a subdomain from getting forced to https and redirected to main url? I'm using the below in my .htaccess file but need a little help as my subdomain (stage.mydomain.com) gets redirected / can't access.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I've tried a few options but no luck - Any ideas? Thanks!
Have your first rule like this:
RewriteCond %{HTTP_HOST} !^stage\.mydomain\.com$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
I got a problem with probably my .htaccess
I use the cakephp framework (2.0) and i use .htacces to redirect a non-www to a www Using this code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is now, when somebody clicks on the link http://mysite.nl/controller/view/E1 They go to http://www.mysite.nl/index.php
instead of http://www.mysite.nl/controller/view/E1 (with www)
And this while the rewrite works on mysite.nl to www.mysite.nl
Can somebody tell me what i do wrong? i have searched for different kind of rewrite rules but with no luck.
Full .htaccess file after comment:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Ordering of rules is very important in .htaccess. In general your 301s should appear before front controller rules that catch all the requests and change the request URI.
Try this code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# add www rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# cakephp front controller rule
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>