i would like to use the following rule in my htaccess to forward one url to another but it won’t work. Could anybody help me?
RewriteCond %{HTTP_HOST} ^(.*)mydomain\.com [NC]
RewriteRule /cms-page.html http://www.mydomain.com/second-cms-page.html [L,R=301]
the following rule is working, but it would forward the whole domain
RewriteCond %{HTTP_HOST} ^(.*)mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/second-cms-page.html [L,R=301]
and i can’t use “RedirectPermanent” because there i can’t specify a host. This is necessary because there are multiple domains accessing the same htaccess
Remove leading slash from your regex:
RewriteCond %{HTTP_HOST} mydomain\.com [NC]
RewriteRule ^cms-page\.html$ http://www.mydomain.com/second-cms-page.html [L,R=301,NC]
.htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.
Related
I need to redirect a few (not all) subdomain pages to another address in .htaccess:
How can I redirect the following:
spb.example.com/blog/
ekb.example.com/blog/
spb.example.com/projects/
ekb.example.com/projects/
to
example.com/blog/
example.com/projects/
I've tried:
RewriteCond %{REQUEST_URI} ^spb.example.com/blog/$
RewriteRule ^.*$ https://example.com/blog/? [R=301,L]
But it isn't working.
RewriteCond %{REQUEST_URI} ^spb.mytestsite.com/blog/$
RewriteRule ^.*$ https://mytestsite.com/blog/? [R=301,L]
The REQUEST_URI server variable contains the URL-path only. It does not contain the requested hostname (which is present in the HTTP_HOST server variable).
For example:
# Redirect "spb.example.com/blog/" to "example.com/blog/"
RewriteCond %{HTTP_HOST} ^spb\.example\.com [NC]
RewriteRule ^blog/$ https://example.com/blog/ [R=301,L]
However, you can do all 4 redirects in a single rule by using a more flexible regex and backreferences.
For example:
RewriteCond %{HTTP_HOST} ^(?:spb|ekb)\.example\.com [NC]
RewriteRule ^(blog|projects)/$ https://example.com/$1/ [R=301,L]
The $1 backreference contains either blog or projects from the requested URL-path (captured in the RewriteRule pattern).
NB: Test first with 302 (temporary) redirects to avoid potential caching issues.
UPDATE:
How can I redirect all blog articles from subdomains the same rules For example: from spb.example.com/blog/my-blog-article-1/ ekb.example.com/blog/my-blog-article-1/ to example.com/blog/my-blog-article-1/
You could do it like this by adding a second capturing group to the existing rule above:
RewriteCond %{HTTP_HOST} ^(?:spb|ekb)\.example\.com [NC]
RewriteRule ^(blog|projects)/(.*) https://example.com/$1/$2 [R=301,L]
I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.
Here is what I need to redirect to a temporary HTML page:
http://www.domain1.com/?Itemid=230
should get redirected to:
http://www.domain2.com/temoporary-solution.html
Here is what I came up with, just not sure if it will cause any issues between the rest of the .htaccess rules (this is the first rule):
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} ^Itemid=230$ [NC]
RewriteRule ^$ http://domain2.com/temoporary-solution.html [R=302,NE,NC,L]
Your rule should work fine. Just append ? at the end of target URI to strip off existing query string:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} ^Itemid=230$ [NC]
RewriteRule ^$ http://domain2.com/temoporary-solution.html? [R=302,L]
Those rules are fine. The conditions are pretty strict so as long as it's the first rule, it won't break anything else.
I am using the following code to redirect wildcard subdomains (*.domain.com) to their coresponding folder in /users and redirect direct requests to the /users folder to the subdomain version:
Protect Direct Access to Wildcard Domain Folders:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
Handle Wildcard Subdomain Requests:
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^(.*)$ /users/%1/$1 [L]
This code works well enough, however there are two problems that I can't seem to fix.
The below scenario seems to happen because there isn't a trailing slash on the requesting URI:
username.domain.com/sub1 => username.domain.com/users/username/sub1
username.domain.com/sub1/ => username.domain.com/sub1/
The users directory can still be accessed directly by using a subdomain:
username.domain.com/users/username/sub1 => Works and shouldn't
I'm at a loss and would really appreciate if anyone has any ideas.
Thank you!
For the problem 2, I think your first protection rule just needs to redirect all subdomains. It's redirecting www, but lets username.domain.com come through as-is.
This will redirect any direct access request to the users path.
RewriteCond %{HTTP_HOST} ^.+\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
I think it can be a little simpler by just looking for any host ending in domain.com (which would even handle no subdomain, just domain.com) (I didn't test this....)
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
For problem 1, I'm stumped too, sorry. It's acting like the trailing slash is failing the rules, so it falls through as-is. But I would expect it to do as you want it to:
username.domain.com/sub1/ => username.domain.com/users/username/sub1/
Perhaps try the %{REQUEST_URI} tag, instead of trying to capture .*. I don't see the difference, but maybe it'll help.
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule .* /users/%1%{REQUEST_URI} [L]
I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]