htaccess - redirect to new domain and subfolder - .htaccess

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).

Related

.htaccess Redirect all subdomains to corresponding folder

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).

ERR_TOO_MANY_REDIRECTS from htacces

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

Remove .html extension from URL only in root

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]

catch22: Needing to remove redirect chain I seem to need

I've been using paths like
<?php include($_SERVER['DOCUMENT_ROOT'].'/directory/file.php'); ?>
in my pages, and it works as expected. However, when I run speed tests with sites like GTmetrix, they all complain about redirects. For example:
Remove the following redirect chain if possible:
h-t-t-p://mysite.com/directory/images/shorter.png
h-t-t-p://www.mysite.com/directory/images/shorter.png
I suppose this is due to these lines in my main .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
So it seems I have a catch22 here. The search engines don't want my site to appear to have duplicate content, and the redirect is a factor in slowing things down.
What is the best practice here? It seems a common situation, but I can't find any solutions by Googling. Should I redefine document_root, so the redirect at the registrar level, or what?
Thanks for your thoughts on this.
Avoid unnecessary static content redirects by updating the src and href attributes in your HTML to use the full domain name including the www.
Or, use absolute paths without the domain name at all.
Or, add a rule to exclude some types of files:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteCond %{REQUEST_URI} !\.(js|css|gif|jpeg|png|jpg|pdf|ico)$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

.htaccess rewrite add sub-domain exception (remove redirect)

I have checked previous questions, I believe this is quite simple but I can't seem to work it out.
In my .htaccess file I currently have
RewriteCond %{HTTP_HOST} !^www\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
However this is causing a problem as it also redirecting any sub-domains (specifically au.bodycleansediet.com and ca.bodycleansediet.com) to www.bodycleansediet.com
I want them NOT be to be redirected so they can be viewed on their sub-domains.
I know I need to add an exception/re-write rule but I am not sure how to construct it.
Any advice on how to construct this?
Should not something along the following lines work
RewriteCond %{HTTP_HOST} !^(www|ca|au)\.bodycleansediet\.com [NC]
RewriteRule ^(.*)$ http://www.bodycleansediet.com/$1 [R=301,L]
Since you are basically checking if not (www OR ca OR au) then do your redirect.

Resources