add alias in front of each URL mod_rewrite apache - apache

If any likes come in the format
http://localhost/index.php/bla...
I want to convert it to http://localhost/er/index.php/bla...
I'm trying the following but it seems to be looping the url indefinitely
RewriteRule ^localhost/index/php/(.*)$ localhost/er/index.php/$1 [R=301,L]
RedirectMatch 301 ^/localhost/index.php/(.*)$ localhost/er/index.php/$1

You've got 2 different things going on, a RewriteRule (mod_rewrite) and a RedirectMatch (mod_alias). You'll only need one, but neither of those can match against the hostname (localhost). If this has to only be limited to the "localhost" host, then you need to do this:
RewriteEngine On
RewriteCond %{HTTP_HOST} localhost$ [NC]
RewriteRule ^/?index\.php/(.*)$ /er/index.php/$1 [R=301,L]
Otherwise, you can just stick with mod_alias:
Redirect 301 /index.php/ /er/index.php/
Everything after /index.php/ will automatically get appended.

Related

How to redirect from domain to subdomain?

My site is www.mysite.com and I need to redirect any request to us.mysite.com.
So:
www.mysite.com ----> us.mysite.com
www.mysite.com/hello.php ----> us.mysite.com/hello.php
// etc
I tryed this but doesn't work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteRule (.*) http://us.mysite.com$1 [R=301]
It looks like your RewriteCond is only matching domains that start and end with mysite.com. This does not include www.mysite.com.
The following will 301 redirect anything NOT at us.mysite.com to us.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^us.mysite.com$
RewriteRule ^(.*)$ http://us.mysite.com/$1 [R=301]
There are several different solutions. The best one, both from SEO and User perspective, is the one-to-one 301 redirect. It preserves your link juice and at the same time redirects the client to the exact location on the new website.
If you have mod_alias enabled, I would suggest a simple
RedirectMatch 301 ^(.*)$ / http://new.domain.com/$1
The result instruction can be accomplished with
RewriteEngine On
RewriteRule (.*) http://new.domain.com/$1 [R=301,L]
The second one is the best choice if you need to chain multiple conditions and filters. For example, if you need to redirect only certain hosts or clients depending on User Agent header.
From here.

How to combine Apache redirects?

I've got an Apache config that features multiple rewrite rules and redirects in order to get the cutest URLs, prevent duplicates for SEO, etc. Here's a snippet as an example (it features a lot more):
# Redirecting non-www to www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# Removing index.php
RewriteCond %{REQUEST_URI} index\.php$ [NC]
RewriteRule .* / [R=301,L]
# A big bunch of these
Redirect permanent /old-article.html http://www.example.com/new-article.html
Redirect permanent /another-old-article.html http://www.example.com/new-article2.html
This works well, but it happens to generate a lot of redirects. A common case looks like this:
http://example.com/index.php, 301 redirect to http://www.example.com/index.php
http://www.example.com/index.php, 301 redirect to http://www.example.com
It sometimes reaches 4-5 redirects.
Now, I want all these rules to be chained and generate only one 301 redirect, like this:
http://example.com/index.php, 301 redirect to http://www.example.com
I know I can spend an afternoon thinking and sorting the rules to a better match, and also that I can create combined rules. But that would complicate an already long file. I want a flag, operand or whatever that will execute all the rules as if they where internal and only issue the redirect once it has crawled every rule. Is this even possible?
It seems as if simply re-ordering this would get you what you want:
# A big bunch of these
Redirect permanent /old-article.html http://www.example.com/new-article.html
Redirect permanent /another-old-article.html http://www.example.com/new-article2.html
# Removing index.php
RewriteRule ^/index.php http://www.example.com/ [R=301,L]
# Redirecting non-www to www
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
A direct request for an old article at the example.com domain:
http://example.com/old-article.html
Will result in a single redirect to:
http://www.example.com/new-article.html
A request for either http://example.com/index.php or http://www.example.com/index.php will result in a single redirect to:
http://www.example.com/
A request that doesn't match anything else will result in a single redirect from:
http://example.com/foo
To:
http://www.example.com/foo
This seems to cover all the bases. Have I missed anything?
Remove the [L] flag from your RewriteRules, and they will be combined automatically.

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.

Redirect with htaccess for images onto another server without redirect looping

I currently have a host where my main site is hosted on. I have set up nginx on another server to mirror/cache files being requested if it doesn't have it already, in particular images and flv videos.
For example:
www.domain.com is my main site.
www.domain.com/video/video.flv
www.domain.com/images/1.png
I would like to ask apache to redirect it to imgserv.domain.com (imgserv.domain.com points to another server IP)
imgserv.domain.com/video/video.flv
imgserv.domain.com/images/1.png
Basically redirect everything with certain filetypes and preserving the structure of the URL, like flv etc.
I tried something but I am getting a redirect looping error. Could someone help me out?
Thank you!
This is what I have at the moment
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RedirectMatch 302 ^(.*)\.gif$ http://imgserv.domain.com/forums$1.gif
RedirectMatch 302 ^(.*)\.jpg$ http://imgserv.domain.com/forums$1.jpg
RedirectMatch 302 ^(.*)\.png$ http://imgserv.domain.com/forums$1.png
You are mixing up two different modules: RewriteEngine and RewriteCond are from mod_rewrite while RedirectMatch is from mod_alias. They can’t work together.
Try this mod_rewrite example instead:
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*\.(gif|jpg|png)$ http://imgserv.example.com/forums/$0 [L,R]

301 redirect .htaccess

Im trying to request the following entire site 301 redirect:
word.something.blah.domain.com --> http://www.word.com
I don't know how to write the 301 redirect rule.
Can someone help out?
I will assume you are using the same directory to serve files on both domains. In which case, a Redirect clause won't work (infinite redirect loop).
With mod_rewrite, you can check the value of the current HTTP_HOST and take a decision based on that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.something\.blah\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,NE,L]
put this into root directory of the subdomain:
Redirect permanent / http://www.word.com
If you are keeping everything else the same - that is, the file names - but simply changing the domain, this code is all you need to put on the OLD DOMAIN htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.newdomain\.co.uk
RewriteRule (.*) http://www.newdomain.co.uk/$1 [R=301,L]