htaccess redirect all requests from subfolder to subdomain - apache

I have the following (WordPress) URL canonical structure:
http://mysite.com/2011/10/my-post-name/
I want to send all requests from http://mysite.com/2011/... down to the same location on a subdomain.
So specifically that should be:
http://mysite.com/2011/10/my-post-name/ ---> http://subdomain.mysite.com/2011/10/my-post-name/
I'm not sure what the correct syntax is for this, does anyone know?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://subdomain.mysite.com$1 [R=301]
If you also need to match the first part of the path, then use
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule ^/2011/(.*) http://subdomain.mysite.com/2011/$1 [R=301]
You should read the mod_rewrite documentation. It's confusing the first time, but after the 25th time or so it will make pretty good sense and serve you well.

Related

how to dynamically rewrite filepath .htaccess based on domain?

I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]
With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.

What is the correct syntax for "if host is not foo, redirect to bar" in a .htaccess file?

This website has a ton of extra domains (note: these are not subdomains; one of them, for instance, is http://eduard.fi) that the owner (or the SEO people, rather) wants to redirect to the main domain. Instead of listing them one by one, this is what I tried:
RewriteCond %{HTTPS_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301,L]
However this creates a redirect loop. Why is that? This does not produce a server error, so for that part the syntax is correct, but it does not do what I want.
You were close, but made a logical mistake. Take a look at this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^(.*)$ https://masetti.fi/$1 [R=301]
An alternative would be that:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^masetti\.fi$
RewriteRule ^ https://masetti.fi%{REQUEST_URI} [R=301]
The RewriteCond has been slightly altered: It is the variable %{HTTP_HOST} you want to check, not %{HTTPS_HOST}which does not exist.
PS: it is a good idea to start out with a 302 redirection and only change that to a 301 once everything works as intended. That prevents issues with client side caching.

Rewrite part of url with .htaccess

I need to rewrite URL and save top level domain and query.
I tried to use these rules
RewriteEngine On
RewriteBase /
RewriteRule ^domain(.*)$ http://newdomain$1 [R=302,NE,L]
Using this testing tool I found that it works if domain.com/query?param=value is used as a request URL. But if I try to use http://domain.com/query?param=value[ it doesn't work.
Basically I don't care what protocol is (http or https), I just need to replace first occurrence of domain string and rewrite it with newdomain saving all other parts of a request URL.
As it turned out in your comment to the first answer I gave you are actually trying to replace only part of the hostname of the incoming request but keep path and query string. That was not clear to me from your question, sorry.
You have to use an additional RewriteCond for this, since as said before you caanot access the hostname at all inside a RewriteRule. So I guess the following goes into the direction of what you are actually looking for:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ http://newdomain.%1/$1 [R=301,L,QSA]
You may want to try this modification to preserve the original request scheme too:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^[^\.]+\.(.+)$
RewriteCond %{HTTP_HOST} !^newdomain\.(.+)$
RewriteRule ^(.*)$ %{REQUEST_SCHEME}://newdomain.%1/$1 [R=301,L,QSA]

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

how do I redirect from one page to another with mod_rewrite?

All the advice online says do:
rewrite 301 URL-A URL-B
But that won't work if I turn on mod_rewrite (it seems?) with RewriteEngine on
So, I'm bad a regex, but shouldn't need it here. How do I do:
RewriteCond %{HTTP_HOST} ^untamed-adventures.com/travel/How/tabid/58/Default.aspx [NC]
RewriteRule ^(.*)$ http://untamed-adventures.com/ [R=301,L]
%{HTTP_HOST} expands to the host of the request, so it could never match untamed-adventures.com/travel/How/tabid/58/Default.aspx, only untamed-adventures.com.
If you want to forward http://untamed-adventures.com/travel/How/tabid/58/Default.aspx to http://untamed-adventures.com/, try this:
RewriteCond %{HTTP_HOST} =untamed-adventures.com
RewriteRule ^/travel/How/tabid/58/Default.aspx$ http://untamed-adventures.com/ [R=301]
The L flag is redundant; a forward is always final.
Not at all clear what you're trying to do. HTTP_HOST is the hostname part in the requested URL, in this case, "untamed-adventures.com", so that RewriteCond will never match.
I think that what you're trying to do is:
Redirect 301 /travel/How/tabid/58/Default.aspx http://untamed-adventures.com/
In which case, mod_rewrite isn't needed at all.