I have a domain example.com for which I have created two fully separate drupal 8 setup. These two different setups are in two different subfolders under the root, namely, build1 and build2.
A. I want to translate any url to https automatically.
B. I want to add www to any url that does not have www, automatically.
C. When a user accesses the url example.com, I want the request to redirect and open the drupal site that is in the subfolder build1 without revealing build1 subfolder name in the url.
I have achieved A, B, and C by using the following in .htaccess in the root folder.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ build1/$1 [L]
My next objective, which I am struggling is, to add instruction in .htaccess so that when I type in example.com/build2 or www.example.com/build2, it opens up the drupal setup that is in build2.
I have tried for a while but no luck.
Thanks in advance .... Mic
You need to add an exception before the general catch all rule, so further up in your distributed configuration file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,END]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,END]
RewriteRule ^/?build2 - [END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ build1/%{REQUEST_URI} [END]
Depending on the rest of your setup (which is unclear here) you may need to protect requests to existing folders first before rewriting into those applications. Depends, as written...
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,END]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ - [END]
RewriteRule ^/?build2 - [END]
RewriteRule ^ build1/%{REQUEST_URI} [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Related
I'm using this .htaccess to redirect each subdomain to a folder named the same as subdomain and all requests to corresponding index.php.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.com$
RewriteRule ^(/)?$ sub/index.php [L]
It works great, but I'd like it to work with multiple subdomains.
Based on several answers I found in SO, the code should look somewhat like the below, but I can't get it to work.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?(.*).domain.com$
RewriteCond %{REQUEST_URI} !^/(.*)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteCond %{HTTP_HOST} ^(www.)?(.*).domain.com$
RewriteRule ^(/)?$ %1/index.php [L]
Something like this should do wildcard redirect, unless the domain name begins with www.
Note the negative condition using the exclamation mark.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^ http://example.com/%1/index.php [R=301,L]
^(.+?) captures whatever precedes domain.com, unless it is www.
PS: sorry for the last line, it should read domain.com (whatever your actual domain is) instead of example.com. I had to to do this because of posting restrictions on SO.
This should be the straight forward approach:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [QSA,END]
If will redirect all host names within the domain "example.com" and drop that historic "www." prefix people are always so worried about. This does not handle the main domain (with or without "www." prefix), you'd have to add that if you do not operate a separate virtual host for that special one. The index.php stuff should better be handled using the DirectoryIndex directive the apache http server offers. This leads to less complex setups and cleaner URLs.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup, certainly you will need to add another rewriting condition to break an endless rewriting loop.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I want to redirect some subdomains to subdirectories, but want to preserve the requested URL.
For example:
sub1.example.com
Should redirect to /public_html/sub1 and the URL should remain sub1.example.com
I also have a few sub-subdomains, and those should redirect as follows:
nested.sub2.example.com
redirect to /public_html/sub2/nested and the URL remains nested.sub2.example.com
Currently, my htaccess looks like:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)lnested.sub2\.example\.com$ [NC]
#RewriteCond %{REQUEST_URI} !/nested.sub2/
RewriteRule ^ /sub2/nested%{REQUEST_URI} [L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(?!www\.)sub1\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !/sub1/
RewriteRule ^ /sub1%{REQUEST_URI} [L]
I have this 90% of the way there. There are some problems.
If the URL is missing the trailing slash () I sometimes get something like:
sub1.example.com/sub1/
And some other times I just get a 404.
You actually do not want to redirect requests at all, you want to internally rewrite them:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(?:www\.)([^.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteRule ^/?(.*)$ /%1/$1 [L]
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteCond %{HTTP_HOST} ^(?:www\.)([^.]+)\.([^.]+)\.example\.com$
RewriteCond %{REQUEST_URI} !^/%2/%1/
RewriteRule ^/?(.*)$ /%2/%1/$1 [L]
These rules will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Hello Stackoverflow community, I am currently writing my .htaccess file and wondering, which is the best way to combine Rewrite Engine rules.
For example, my .htaccess file right now consists of the following code:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
ErrorDocument 404 /404.html
I want to add the Removing Extension code (for .html):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
As well as the index.html removable code:
RewriteCond %{REQUEST_URI} index\.html
RewriteRule ^(.*)index\.html$ /$1/ [R=301,L]
Which is the appropriate order to type them and how should the final .htaccess file code look like?
Thanks in advance!
I'd say that this is roughly what you are looking for:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^\.]+)$ $1.html [END]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^/?(.+)/?$ /$1/index.html [END]
RewriteRule ^/?(.+/)index\.html$ /$1 [R=301]
ErrorDocument 404 /404.html
You may have to tweek things a little, your question is vague in a few points.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I want to redirect from "example.com", "www.example.com", "example2.com" and "www.example2.com" to "https://example.com/admin_panel/", i tried this:
Options -Indexes
Options +FollowSymLinks
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^www.example2.eu/index\.php
RewriteCond %{THE_REQUEST} ^www.example.eu/index\.php [OR]
RewriteCond %{THE_REQUEST} ^example2.eu/index\.php [OR]
RewriteCond %{THE_REQUEST} ^example.eu/index\.php [OR]
RewriteRule ^(.*)$ http://www.example.eu/admin_panel/index\.php$1 [L,R=301]
</IfModule>
but it does not work
Your question is vague, since it contains contradictions. For example it is unclear how you want to treat the paths in the requests. Is /index.php a requirement for the rewriting to get applied or not?
Given that little and vague information I can only point you into the rough direction:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example2\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^example2\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^example\.eu$
RewriteRule ^/?(.*)$ http://www.example.eu/admin_panel/$1 [R=301]
RewriteCond %{HTTP_HOST} ^www\.example\.eu$
RewriteCond %{THE_REQUEST} !^/admin_panel
RewriteRule ^/?(.*)$ http://www.example.eu/admin_panel/$1 [R=301]
Note that I removed the condition testing for the host name "www.example.eu" since it would lead to an endless rewriting rule.
The given code snippet is only meant as a starting point. It should work, but I did not test it myself. It should work likewise from the http servers host configuration or inside dynamic configuration files (".htaccess").
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
I change the .htaccess file and I added this:
RewriteCond %{HTTPS} !on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but when I try to access the page, it gives me this error:
ERR_TOO_MANY_REDIRECTS
I cleared all my cache and cookies, I tried it with another browser, and it didn't work.
Please help me
It appears that some versions of the apache http servers ssl module do not set the %{HTTPS} variable, quite in contrast to what the documentation claims. Have a try using this alternative:
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Add RewriteEngine On to your first line, vai parecer assim:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
Last line you could change to something like:
RewriteRule ^(.*)$ https://www.example.com/$1
Where https://www.website.com add your own domain.
That should send you on the right way