My current .htaccess file looks like this:
# Use PHP56
AddHandler application/x-httpd-php56 .php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^directoryhub\.net [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.directoryhub.net [R,L]
This redirects directoryhub.net, which is my root domain in public_html, to https.
However, now I want to do the same thing with a second domain (all the files of which are in a subfolder of public_html). How can I achieve this?
Don't use hardcoded host name in condition and target to make it generic for all the domains that are controlled by your .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
I realized that I was editing the .htaccess file in the root folder, which is the wrong file. I needed to edit the .htaccess file within public_html as well as the .htaccess file within each of my subdomains to the following:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain/$1 [R=301,L]
I suppose I could also delete all the .htaccess files of my subdomains and change the root .htaccess to #anubhava's answer, but it works by editing each individual file as well.
Related
I want to redirect all the urls of a directory (example.com/blog/urlnames) on one domain to a sub-domain (blog.example.com/urlnames)
I tried:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^/blog$ [NC]
RewriteRule (.*) https://blog.example.com/$1 [R=301,L]
I keep getting a 404 error when I type the url on example.com:
https://example.com/blog/5-names-for-actors/
I want this to go to:
https://blog.example.com/5-names-for-actors/
You can use this rule in blog/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://blog.example.com/$1 [R=301,L,NE]
File blog/.htaccess may not be existing already so you may have to create it.
I don't know much about the .htaccess file and need some assistance please. I have a few websites hosted one one CPanel account. I didn't want to have my "main" site stored directly in the public_html folder, so I moved it into a subfolder and then found this .htaccess configuration that allows me to load my main site from that subdirectory. So that is working.
# .htaccess main domain to subdirectory redirect
# Do not change this line.
RewriteEngine on
# Change example.com to be your main domain.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteCond %{REQUEST_URI} !^/example_directory/
# Don't change the following two lines.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Change 'subdirectory' to be the directory you will use for your main domain.
RewriteRule ^(.*)$ /example.com/$1
# Change example.com to be your main domain again.
# Change 'subdirectory' to be the directory you will use for your main domain
# followed by / then the main file for your site, index.php, index.html, etc.
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/index.html
So as I said, the above .htaccess will allow me to store my site files in a different folder rather than right at the root. But now I'm trying to force a redirect for this site over https as well. I found this site here:
Force Website to Use SSL
But, since I really don't understand the .htaccess file I'm having trouble integrating these two features together. Any help on this would be greatly appreciated. Thanks.
I'm pretty certain I figured this out myself. I just simply had to add this to the end of my .htaccess file:
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
and it works like a charm. Full .htaccess file looks like this, which changes the folder directory for the site to use and then forces it over https.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/example_directory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /example.com/$1
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteRule ^(/)?$ example.com/index.html
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}
This is driving me bananas. For some reason my domain will not work to redirect https-WWW to https-non-WWW. This works with every other permutation except https://www.nextoronto.com
What code do I use in the .htaccess that will allow it to work for all permutations?
Edit: This is the rewrite code now:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.nextoronto\.com [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [L,R=301]
It looks sound, perhaps that rule isn't being reached due to other rules in front of it?
Here is what I use that works:
# Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://nextoronto.com/$1 [R=301,L]
# My other rewrites for routing all requests to index.php go here...
Edit the URL and try with this code:
RewriteEngine on
rewritecond %{http_host} ^www\.example\.com [nc]
rewriteCond %{SERVER_PORT} 80
rewriterule ^(.*)$ https://example.com/$1 [r=301,nc]
Step 1
You should make .htaccess file like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1
Step 2
Go to the directory, then view mark the option file extension. Then see the .htaccess file.
When no rewrite works, no matter what you do, consider what happened to me. IIS or WAMP, on my laptop, redirected all calls to my domain (call it example.com) to localhost. So creating all the rewrites on my example.com server were to no avail because no calls to example.com ever made it out of my computer. Just do a "ping example.com" and if you get an IP of 127.0.0.1, edit your HOSTS file.
straight to the point.
I have a web hosting with 2 domains pointing to the root directory of the hosting.
I have 2 seperate projects in the root directory both symfony2
I need to redirect the domains to the seperate directories using .htaccess.
My current .htaccess file in the root folder
RewriteEngine On
#www to non www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301, L]
#domain1
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain1/web
RewriteRule ^(.*) /domain1/web$1 [L]
#domain2
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !/domain2/web
RewriteRule ^(.*)$ /domain2/web$1 [L]
now when I type in domain1 in the adress bar. The project works correctly. What I get in the adress bar is: "domain1.com/domain1/web" And I'd like to get rid of the domain1/web part so that the functionality stays. Do I somehow use rewrite base? Or is there an other way for me to get my result? please help. Note that I'm not really used to working with .htaccess so please be gentle :)
I would like to modify my .htaccess file so that when someone comes into my site without typing www the site always redirects them to the www version. For example, if my url is www.abc.com and they just type abc.com, I want to redirect them to abc.com.
Here is my current htaccess file:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Normally I know how to do the redirect, but im having issues since it already has those few lines in there.
I use the code below. It can be used for any domain name. You just need to enter it in your .htaccess file.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
(edited to have all the code in the same block)
Add something like this immediately after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
There are two methods available
1) use mod_alias apache module
Redirect permanent /something http://yourwebsite.com/something
2) Add the following entry in your .htaccess / http.conf / yourwebsite.conf in the webserver configuarion directory
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourwebsite.com
RewriteRule ^(.*)$ http://www.yourwebsite.com$1 [R=permanent,L]
If you want redirect example.com to www.example.com you can try below code
RewriteEngine on
RewriteCond %{HTTP_HOST} !www.example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]