RewriteRule help - apache

I have a URL
http://test.devsite-1.com/test/tbox/
which I want to redirect to
http://tbox.devsite-1.com/
Rule:
RewriteCond %{HTTP_HOST} !^tbox\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.|)(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/tbox(/.*|)$
RewriteRule /tbox/(.*) http://tbox.%{HTTP_HOST}/$1 [R=301,L]
I don't understand why it is not redirecting me to the URL? Please note I need a generalized rule so that if I change test.devsite-1.com to tempo.devsite-1.com the same should work with the other URL as well.

Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.(.+)$ [NC]
RewriteRule ^test/tbox/(.*)$ http://tbox.%1/$1 [R=301,L]
This will redirect (301 Permanent Redirect)
http://test.devsite-1.com/test/tbox/something-optional
to
http://tbox.devsite-1.com/something-optional
It has to be placed in .htaccess file in website root folder (e.g. http://test.devsite-1.com/.htaccess). If placed elsewhere some tweaking may be required.
It will only work if request is coming via test. subdomain.
And it will only work if URL requested starts with test/tbox/.
All of the above matches your URL examples.

Related

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Folder path is dropped when redirecting with htaccess

I am redirecting all non-www to www.
It works fine for all links unless there is a link that contains a folder in our root directory, like /us/ or /uk/.
This is my rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
If I use tools like this one: http://htaccess.madewithlove.be/ it shows that the redirect is working fine:
http://example.com/uk/ redirects to http://www.example.com/uk/
But in reality it redirects to http://www.example.com/. I deleted every cache and varnish and also used tools like this: http://www.redirect-checker.org/index.php and cURL.
The folder path is being dropped. http://example.com/uk/whatever becomes http://www.example.com/whatever
What else could it be?
I inserted my redirects also in a .htaccess file inside the /uk/ folder.
Thanks!
I inserted my redirects also in a .htaccess file inside the /uk/
folder.
You can not do that. It just gives the wrong result you get.
Use yours or this htaccess at root:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
You can try to use this rewrite rule instead:
RewriteCond %{HTTP_HOST} ^([a-z.]+)?yoursite\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%yoursite.com%{REQUEST_URI} [R=301,L]

Apache wildcard subdomains with url masking

I am trying to handle wildcard subdomains with url masking in apache.
Correct rewrite rules should achieve the following:
http://demo.system.dev to to http://system.dev?subdomain=demo
http://sample.system.dev/user/edit/100 to to http://system.dev/user/edit/100?subdomain=sample
http://debug.system.dev/project/edit/new to to http://system.dev/project/edit/new?subdomain=debug
So far i have the following rule in my .htaccess
RewriteCond %{HTTP_HOST} ^(.*)\.system\.dev
RewriteRule ^(.*?)$ http://system.dev%{REQUEST_URI}?subdomain=%1 [L]
which looks like its working ok except that the browser url is also changed. I would like the browser url to remain the same and internally route the request but i am not sure how to achieve this.
Try this rule in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} !(?:^|&)subdomain= [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev$ [NC]
RewriteRule (.*) $0?subdomain=%1 [L,QSA]
You can not internally rewrite to another domain. So when you go to your subdomain it will redirect to main domain as you have it. So you will need to use relative URL and see if that works for you.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www\.)?system\.dev [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.system\.dev [NC]
RewriteRule ^(.*)$ %{REQUEST_URI}?subdomain=%1 [L]

RewriteRule showing inconsistence behavior

I have the following rule in my htaccess file (this is the full htaccess file):
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST} [L,R=301]
Using the htaccess tester (http://htaccess.madewithlove.be/) it shows the non-www URL is correctly redirected to a www url (test with mydomain.com/ and mydomain.com/subdirectory/ (goes to www.mydomain.com/subdirectory/).
Now, when I put this htaccess file on my site, it will redirect mydomain.com/subdirectory/ to www.mydomain.com instead of to www.mydomain.com/subdirectory/
Why does it show this inconsistent behavior?
You're not using capture value $1 in target:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
So, I looked up what the HTTP_HOST value actually is. It looks like it doesn't contain any further than the domain extension (i.e. domain.com but not anything that's behind that) so it will redirect to domain.com)
I modified the htaccess to the following, which is working:
RewriteCond %{HTTP_HOST} ^(?!www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
If anyone still has any suggestions how to improve the regex, I'm open for improvements

Trying to redirect blog.domain.com to www.domain.com/blog/

I'm combining a main site at www.domain.com, and an old wordpress blog at blog.domain.com, into one completely new Wordpress install. I have exported and imported all the old blog posts so that they now live under wwww.domain.com/blog//
I am trying to create one rewrite rule that will map all the old blog posts to their new URLS.
I've tried variations on these SO discussions:
DNS/.htaccess files to redirect subdomain to specific folder
Apache rewrite rule different if capture is empty
but nothing is working.
The following in my .htaccess file will redirect blog.domain.com to www.domain.com/blog, if there is nothing more in the URL:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/?$ http://www.domain.com/blog/$1 [R=301,L]
but if there is anything more in the URL, it does not rewrite the URL at all, and goes to the new Wordpress site's 404 page.
I tried adding a capture to the final RewriteRule line, but then no rewrite occurs, and it just goes to the new homepage but the address bar still reads "blog.domain.com":
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^/(.+)$ http://www.domain.com/blog/$1 [R=301,L]
Is there a way to do what I'm trying to do?
You want to remove the leading slash:
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/blog/$1 [R=301,L]
# no slash---^
And make the + a *. URI's that are sent through rules in htaccess files have the leading slash stripped off, so ^/(.+)$ would never match.
Replace your rule to this:
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.domain\.com$ [NC]
RewriteRule ^ http://www.domain.com/blog%{REQUEST_URI} [R=301,L]
PS: Make sure this is the 1st rule in your .htaccess.