After some changes on my wordpress site, ther are many links out there in the web which cant be accessed anymore. The old urls look like
http://example.com/blog/2017/05/post ans should now show this url
http://example.com/post
I tried this code in .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([^/]+)/([^/]+)/?$ $1 [R=301,L]
This doesnt work. Than tried this one.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/././?$ $1 [R=301,L]
This also doesnt work.
Most likely this is what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?blog/([^/]+)/([^/]+)/(.+)/?$ $1 [R=301,L]
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).
Related
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).
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).
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'm trying to remove the .html file extension from my URL's to make them look nicer. I've seen many examples of this and have tried them, but I'm struggling to find something that only works in the root, and doesn't apply to any subdirectories or subdomains.
Can I get any help with this?
Example:
example.org/test.html > example.org/test
example.org/food/xyz.html > example.org/food/xyz.html
login.example.org/something.html > login.example.org/something.html
Your question is a bit vague, so I will concentrate on the main issue you mention:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/.+\.html
RewriteRule ^/?([^/]+)$ $1.html [L]
Alternative approach:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^/]+)$ $1.html [L]
If this is limited to only a single host name (as your examples might suggest) depends on your setup. Such thing is easiest done if you place the rule in the real host configuration of your http server instead of using .htaccess style files. That is also why I append a general hint:
You should always prefer to place such rules inside the http servers host configuration instead of using .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).
In the comment you wrote that you also want to force the shorter notation, so redirect users request a URL with such "file name extension" to the version without.
I don't really see any point in this, but it certainly is possible:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !^.
RewriteRule ^. - [L]
RewriteCond %{REQUEST_URI} ^/.+\.html$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?(.+)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/.+\.html$
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^/]+)$ /$1.html [L]
even there are tons of articles about that problem i can not get this to work.
My situation is like this:
From one Domain e.g. english-domain.com i want a rewrite to the another domain into a specific directory e.g. german-domain.de/en. And each of these domains contains a hyphen. The typical generators are sadly not providing me with a working solution.For example the following code does not work
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?english-domain\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ german-domain.de/en/$1 [R=301,L]
It would be very thankful if you guys could point me to correct direction.
Thank you
What about this simply modified version?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?english-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://german-domain.de/en/$1 [R=301,L]
I wonder though if that second RewriteCond you coded really is what you want. It will limit the rewriting to only the specific URL https://english-domain.com/, not to URLs having more than just the base / as path component... If you want to rewrite all URLs in that host, so for example also https://english-domain.com/foo/bar, then you need to remove that condition:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?english-domain\.com$ [NC]
RewriteRule ^(.*)$ https://german-domain.de/en/$1 [R=301,L]
Oh, and obviously you have to make sure that the interpretation of htaccess style files is enabled at all and the rewrite module is installed.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .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).