Redirect URL alternatives with htaccess - .htaccess

I am able to redirect URL to another URL with htaccess by using the following directive:
RewriteCond %{REQUEST_URI} ^/example$
RewriteRule (.*) /pages/example [L,R=301]
RewriteCond %{REQUEST_URI} ^/example
RewriteRule (.*) /pages/example [L,R=301]
But I want to make this with a single directive in htaccess and this directive should cover all alternatives;
http://mydomain.com/example -> http://mydomain.com/pages/example
http://mydomain.com/example/ -> http://mydomain.com/pages/example
http://mydomain.com/example/test -> http://mydomain.com/pages/example/test
How can I do this with a single .htaccess? Any thoughts?
Thanks for your help.

Try this :
RewriteRule ^example(/(test)?)?$ /pages/example$1 [L,R=301]

Related

.htaccess - Rewrite query string and redirect to directory

I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>

Htaccess redirect subdomain doesn't work

I have a subdomain called es and I need when someone wants to enter mysite.com/es it can be redirect to es.mysite.com. It works with the following htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com$ [NC]
RewriteRule ^(.*)$ http://es.mysite.com/$1 [R=301,L]
RedirectMatch permanent ^/es/?$ http://es.mysite.com/$1
The problem is when someone types mysite.com/es/bla/bla/bla. In this case, with the current configuration on my htaccess, the user isn't redirected and I want the user can be redirected.
For example:
If I enter:
http://letsbonus.com/es/barcelona/spa-experiencie-para-2-opcion-masaje-desconecta-roc-nature-273710
This is redirect to:
http://es.letsbonus.com/barcelona/spa-experiencie-para-2-opcion-masaje-desconecta-roc-nature-273710
Thanks in advance.
You need just this one rule in your root .htaccess of mysite.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(mysite\.com)$ [NC]
RewriteRule ^es/(.*)$ http://es.%1/$1 [R=301,L,NE]
Your two rules enter into conflict:
Base url: http://example.com/es/test
Non-www redirection -> http://www.example.com/es/test
es subdomain redirection -> http://es.example.com/test
Non-www redirection... (we're no longer under www subdomain)
I would use this htaccess to get the excepted result:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www|es).example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^es/(.*)$ http://es.example.com/$1 [R=301,L]

htacces URL rewrite redirection

I have a link: http://testsite.com/api/v2/1
I would like to point my browser to: 1.testsite.com and using redirection in .htaccess link it to this place: http://testsite.com/api/v2/1
How can I do this?
Try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.testsite\.com$ [NC]
RewriteRule ^$ http://testsite.com/api/v2/%1 [L]

htaccess redirection subdomain

I want to redirect feedback.domain.de to www.domain.de/de/abc/cde.html via htaccess.
My current htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.de$
RewriteRule ^(.*)$ http://www.domain.de/$1 [L,R=301]
SO, I thought just to add:
redirect 301 feedback.domain.de www.domain.de/de/abc/cde.html
but it doesn't work. If i try to open feedback.domain.de it redirects to www.domain.de
I know this is a very easy question but I don't how solve it in htaccess :-(
The result I want is:
domain.de -> www.domain.de/de/index.html
www.domain.de -> www.domain.de/de/index.html
domain.de/de/example.html -> www.domain.de/de/example.html
etc...
feedback.domain.de -> www.domain.de/de/feed.html
Best regards
The Redirect directive only accept a path relative to the root path (for example /my-path) and won't match on the host part of the URL.
Try this (note that here I assume you want to redirect domain.de to www.domain.de and not all subdomains):
RewriteEngine On
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.domain\.de$
RewriteRule . http://www.domain.de/de/abc/cde.html [L,R=301]
# Redirect domain.de to www.domain.de
RewriteCond %{HTTP_HOST} ^domain\.de$
RewriteRule . http://www.domain.de/$0 [L,R=301]
Updated answer (2013-03-18):
# Redirect feedback.domain.de to http://www.domain.de/de/abc/cde.html
RewriteCond %{HTTP_HOST} ^feedback\.(domain\.(de))$
RewriteRule .+ http://www.%1/%2/feed.html [L,R=301]
# domain.de -> www.domain.de/de
RewriteCond %{HTTP_HOST} ^(domain\.(de))$
RewriteRule .+ http://www.%1/%2/$0 [L,R=301]
# www.domain.de/de -> www.domain.de/de/index.html
RewriteCond %{HTTP_HOST} ^www\.domain\.(de)$
RewriteRule ^%1/?$ index.html [L,R=301]

htaccess 301 redirect dynamic url

I am moving my site to new domain. Need to redirect pages
from
old-site.com/oldpage.php?id=X
to
new-site.com/newpage-X
(X is number)
Why this rule does not work?
RewriteEngine on
RewriteRule ^oldpage.php?id=(.*)$ http://new-site.com/newpage-$1 [R=301,L]
The RewriteRule does only operate on the URL path and not the URL query. You need to use the RewriteCond directive to test the URL query (%{QUERY_STRING}). So try this rule:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)id=([^&]+)&?(.*)?$
RewriteRule ^oldpage\.php$ http://new.example.com/newpage-%3?%1%4 [L,R=301]
This rule will also preserve other parameters in the query.
I suspect that you will need to use QUERY_STRING
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^oldpage\.php$ http://new-site.com/newpage-%1 [R=301,L]
Hope this helps

Resources