Apache mod_rewrite: can these simple RewriteRule be improved? And suggestions! - apache

I started finally to understand Apache mod_rewrite. It's pretty GREAT!
Plz have a look at the followings:
1) Permanent redirects http://www.domain.com/folder_name/ (with or without final slash and with or without the www) to http://www.domain.com/some/path/some_page.html
RewriteRule ^folder_name[/]*$ "http\:\/\/domain\.com\/some\/path\/some_page.html" [R=301,L]
2) Permanent redirects all requests to www.domain.com... to same path and file request but without www in domain
RewriteCond %{HTTP_HOST} !^domain.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.com\/$1" [R=301,L]
They all work as expected and do their jobs, I'm simply curios if some guy, who is more expert than me in mod_rewrite, could give me some advises like: "it could be better in this way...", "there might be a problem if...", etc.
Thanks!

Use the ? quantifier instead of * and you don’t need to escape the substitution URL:
RewriteRule ^folder_name/?$ http://example.com/some/path/some_page.html [R=301,L]
You might want to consider HTTP 1.0 requests where the Host header field is missing. Another useful extension would be to take HTTPS into account:
RewriteCond %{HTTP_HOST} !^(|example\.com)$
RewriteCond %{HTTPS} ^on(s)|
RewriteRule ^ http%1://example.com%{REQUEST_URI} [R=301,L]

Related

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 one directory to another .htaccess

I want to automatically redirect http requests to news/images to ../images.
Is that possible with .htaccess?
Thing is: request to www.site.tld/news/images ... should go to www.site.tld/images ...
I have tried:
RewriteEngine On
...
...
RewriteRule (.*)news/images(.*) ../images [R=301,L]
not working.
I have ensured that apache have mod_rewrite.c enabled.
To redirect all requests for /news/images/ to /images/, capture the part after images and use it in the RewriteRule
RewriteRule ^news/images(.*)$ /images$1 [R,L]
When it works as it should, you may replace R with R=301. Never test with R=301.
You can use:
RewriteRule ^www\.site\.tld/news/images$ /www.site.tld/images?&%{QUERY_STRING}
or you can also use:
RewriteCond %{HTTP_HOST} ^www.site.tld/news/images$ [NC]
RewriteRule ^(.*)$ http://www.site.tld/images/$1 [R=301,L]
But as #arkascha said, please do some research first, there are MANY answers to this sort of problem! :) Either way, I hope this helps.

Apache redirect from one subdomain to another

Does anyone know a way to do a permanent redirect from a.example.com to b.example.com? I have other subdomains that need to remain as they are though. so:
first.example.com -> second.example.com
third.example.com is fine
fourth.example.com is fine
I want to do this in the <VirtualHost> block rather than an .htaccess to avoid having to redeploy again.
Any thoughts would be appreciated, thanks
Add the following RewriteRule to your VirtualHost
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule ^ http://second.example.com [R=301,L]
If you wanted to redirect first.example.com/some/url to second.example.com/some/url:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^first.example.com$
RewriteRule /(.*) http://second.example.com/$1 [R=301,L]
Recommend you use [R=302] whilst testing the rules to avoid problems with 301s getting cached by your browser.

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.