I've got the following code that will redirect "www.domain.com" to "domain.com". This is fine but I want it to redirect all subdomains, ie. "test.domain.com" to "domain.com" as well. It has to be dynamic, meaning that the domain name can't be hardcoded.
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
I've tried various stuff but I can't get it to work.
This will do it:
RewriteCond %{HTTP_HOST} ^[^.]+\.([^.]+\.[^.]+)$
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
It supports anything.example.com and redirects to example.com and example.com can be served on the same site using the redirect.
Related
I'm having a heck of a time constructing an .htaccess file to do everything that I need. I have a domain that is only hosting a forum in a /forums subdirectory. I have no landing page, nor do I intend to.
So I want to redirect the root domain to the /forums subfolder, force https and THEN PREVENT naked domains from being allowed.
I've got it all working except users can still manually access/choose a naked domain variant. Some users have been bookmarking the naked domain variant and I want to force them, and all users, back into the www. prefix.
The code below takes http:www.example.com or http:example.com and forces it to https://www.example.com/forums/. The problem is users are still able to manually access https://example.com/forums.
I've tried a bunch of different things to force the forums in the /forums subdirectory to always use a www. prefix, but nothing has worked thus far:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule (.*) http://www.example.com/forums [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^/?$ "https\:\/\/www\.example\.com\/" [R=301,L]
With your shown samples/attempts could you please try following. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
##Handle non https all urls here.
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
##Handle non www urls here.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
##Handle no uri link with no www page.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^/?$ http://www.example.com/forums [R=301,L]
##Handle no uri link with www page.
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^/?$ https://www.example.com/ [R=301,L]
I have a bunch of different websites on my server and i'm currently using this redirect rule in my htaccess for all of them
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
the htacces files are exactly the same. but for some reason only some of the sites are redirecting www to nonwww and some aren't. Any reason this works for some sites and doesn't work for others?
whats strange is that when i go to www.example.com it doesn't redirect. But when I click on a link on that page such as www.example.com/login. It then redirects the www to https://example.com/login even though the link is www.example.com/login
EDIT: I use the codeigniter framework and have this rule above it to remove the index.php in the url
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
I have a domain "example.com" and i use following redirection code to redirect it to www in .htaccess file
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
it is working fine until i generate a sub-domain with that domain like "abc.example.com" but it's getting conflicts with htaccess and redirecting the sub-domain to "www.abc.example.com/abc/"
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|abc)\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
I have a big issue here.
I have a site example.com, due to the way it was coded, example.com is different from example.com/index. How can i use htaccess to redirect all incoming request from example.com to example.com/index
Here is what i've tried to do but it's not working
The first i did was
RewriteRule ^(.*)$ http://%1/index/$1 [R=301,L]
The second was
RewriteRule ^/index/([^/.]+)/?$ index.php [L]
I'm not so familiar with htaccess though. Any help?
Thanks
NOTE there are so many domain the .htaccess file is working for and i want the rule to affect all e.g example.com should go to example.com/index, example1.com should go to example1.com/index and ... like that
And i already have this rule on top RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
You can use this single rule in the website root .htaccess:
RewriteEngine On
RewriteRule ^/?$ /index [L,R=302]
This will redirect:
example.com => example.com/index
example1.com => example1.com/index
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule ^(.*)$ http://example.com/index.php/$1 [R=301,L]
I want to make a URL change whenever clients access my website with example.com to wwww.example.com I have used .htaccess for this but it works not correct.
Because now a-z0-9.example.com wil also redirect to www.example.com, I want this only works for example.com than redirect to www.example.com
.HTACCESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.eample.com/$1 [L,R=301,NC]
Make another .htaccess for a-z0-9.example.com with the code bellow:
RewriteEngine on
RewriteCond %{HTTP_HOST} a-z0-9.example.com [NC]
RewriteRule ^(.*)$ http://www.a-z0-9.example.com/$1 [L,R=301,NC]
I think this will work.
Good Luck!