htaccess Redirection page - .htaccess

Is it possible to do a redirect (->) in de .htaccess-file, the following way? :
www.website1.com/blog/2020/abc -> www.website2.com/blog/2020/abc
www.website1.com/blog/2021/rst -> www.website2.com/blog/2021/rst
www.website1.com/blog/2022/xyz -> www.website2.com/blog/2022/xyz
Here is my sample of how I am doing this
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/blog/(.*)$
RewriteRule ^(.*) https://website2.com/blog/%1 [R=301,NC]
Thanks in advance,
Vandna

Related

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]

Redirect URL alternatives with 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]

multiple redirects with .htaccess

I want to do this
website.com/blog/index.php -> website.com/blog
website.com/admin/archief_login.php -> website.com/admin
this works with my code.
but I want to add this:
website.com/aa -> website.com/web/index.php?init=aa
for some reason the blog gets this redirect: website.com/blog/?init=blog
what is the best way to set these different rewrites?
RewriteEngine on Options All -Indexes
RewriteCond %{HTTP_HOST}
^websit.com$ [OR] RewriteCond
%{HTTP_HOST} ^www.website.com$
RewriteRule ^admin$
"http\:\/\/www.website.com\/admin/archief_login.php"
[R=301,L]
RewriteRule ^blog$
"http\:\/\/www.website.com\/blog/index.php"
[R=301,L]
DirectoryIndex client_login.php
RewriteRule
^screen-([a-zA-Z0-9_-]+).html$
index_client.php?screen=$1
RewriteRule
^invoice([a-zA-Z0-9_-]+).html$
make_invoice.php?id=$1
RewriteRule
^pack-([a-zA-Z0-9_-]+).html$
index_client.php?screen=pack_code&wwwcode=$1
You need to put the more "general" rules lower in the file so they don't match almost all of your URLs
RewriteRule ^(\w)$ /web/index.php?init=$1 [L, NC]
RewriteRule ^blog$ /blog/index.php [R=301,L]
The above will do
website.com/aa => website.com/web/index.php?init=aa
website.com/blog => website.com/web/index.php?init=blog
If you reverse the two rules you will get
website.com/aa => website.com/web/index.php?init=aa
website.com/blog => website.com/blog/index.php

Updated the .htaccess and website doesn't load now.. what shall i put in to restore it?

thanks a million! my website is not loading now .please. please.please!
Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request.
Well, now that i solved the loading page problem. I'll explain myself better...
all i wanted to do is those redirections:
http://www.domain.com/ -> http://domain.com/
http://domain.com -> http://domain.com/
http://domain.com/article/55/any-name-here-wont-even-check -> http://domain.com/page.php?id=55
Mi go was (the last redirect it's an old->new domain name. It's also done through cPanel):
RewriteEngine On
#Options +FollowSymLinks
RewriteBase /
RewriteRule ^article/(.+)/(.+) page.php?id=$1
RewriteCond %{HTTP_HOST} ^oldname.com$
RewriteCond %{REQUEST_URI} ^oldname.com [NC]
RewriteRule ^(.*)$ http://newname.com/$1 [L,R=301]
Is correct? if so, any idea what could be wrong?
Try this out and see if it works for you.
RewriteEngine on
RwritteBase /
RewriteRule ^article/([^/\.]+)(/.*)?$ page.php?id=$1
RewriteCond %{HTTP_HOST} ^oldname\.com
RewriteCond %{REQUEST_URI} ^oldname\.com
RewriteRule (.*) http://newname.com/$1 [R=301,QSA,L]

htaccess rewrite only part of the querystring

I am trying to perform the following rule in htaccess:
www.domain.com/folder/?id=14077&c=en-gb -> www.domain.com/folder/?id=14077
www.domain.com/folder/?c=en-gb&ID=14077 -> www.domain.com/folder/?id=14077
www.domain.com/folder/?id=14077&c=fr-fr -> www.domain.fr/folder/?id=14077
www.domain.com/folder2/?c=fr-fr&ID=14077 -> www.domain.fr/folder2/?id=14077
www.domain.com/folder2/?c=en-us&ID=14077 -> www.domain.us/folder2/?id=14077
Basically take out the "c" part of the querystring and redirect it to a new domain, based on the following rules:
c=en-gb -> www.domain.com
c=fr-fr -> www.domain.fr
c=en-us -> www.domain.us
Any help welcome!
Admittedly, I'm not sure if this is a task best solved by mod_rewrite...but what the hell, why not:
(Not fully tested, but it seems to work well)
RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)(\A|&)c=([A-Za-z\-]+)&?(&.*)?$
RewriteRule .* - [E=SWITCHLANG:%3,E=QSONE:%1,E=QSTWO:%4]
RewriteCond %{ENV:SWITCHLANG} =en-gb [NC]
RewriteCond %{HTTP_HOST} !(.*)\.com$
RewriteRule (.*) http://www.domain.com/$1?%{ENV:QSONE}&%{ENV:QSTWO}
RewriteCond %{ENV:SWITCHLANG} =fr-fr [NC]
RewriteCond %{HTTP_HOST} !(.*)\.fr$
RewriteRule (.*) http://www.domain.fr/$1?%{ENV:QSONE}&%{ENV:QSTWO}
RewriteCond %{ENV:SWITCHLANG} =en-us [NC]
RewriteCond %{HTTP_HOST} !(.*)\.us$
RewriteRule (.*) http://www.domain.us/$1?%{ENV:QSONE}&%{ENV:QSTWO}

Resources