.htaccess redirect folder to a url - .htaccess

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.

By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]

here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/

I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]

I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

Related

How can I redirect old URLs to new URLs using htaccess? [duplicate]

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

301 redirect a folder but exclude child folder

I need to redirect a directory "mysite.com/blog" to a new site. However, I do NOT want to redirect a child folder "mysite.com/blog/wp-admin".
Here is my htaccess code which effectively redirects the entire /blog folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
</IfModule>
I have tried the following to exclude /wp-admin, but that didn't work:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/blog/wp-admin(.*)
RewriteRule ^blog(.*) http://www.newsite.com/newblog [L,R=301]
Any ideas? Thank you!
UPDATE:
I tried removing the (.*) from both lines, as directed, and its still redirecting to the new site. BUT, the URL is different. Here's the URL once it redirects:
http://www.newsite.com/newblog/?redirect_to=http%3A%2F%2Foldsite.com%2Fblog%2Fwp-admin%2F
Can you tell from this URL what's going wrong? I'm not good at regex, but it looks like it's a formatting issue.

Apache Server .htaccess rewrite with wildcard

Is it possible to create a rewrite code that includes a wildcard?
for example any request for:
http://example.com/something1/something2/*
would redirect to:
http://example.com/newthing
AND
will it matter where in the .htaccess file I place the rewrites?
This should work for you:
RewriteEngine On
RewriteRule ^something1/something2/(.*)$ /$1 [R=302,L]
It should change http://example.com/something1/something2/test to http://example.com/test
EDIT:
RewriteEngine On
RewriteRule ^shirts/(.*)$ /clothing.html [R=301,L]

.htaccess redirect to a different subdomain

Please i need help to create a correct instruction in .htaccess to redirect old urls done in this way:
/it/cartelle/465-piquadro-signo-cartella-ca1744si.html
to
/it/valigeria/cartelle/465-piquadro-signo-cartella-ca1744si-ca1744si.html
in other words we have shifted all category cartelle under category valigeria.
Our .htaccess at the moment is:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.domain.it$
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
Anyone can help me?
Thanks
You can try this if you want to redirect to a new URL:
RedirectMatch /it/cartelle/(.*) /it/valigeria/cartelle/$1
If you want to keep the old url and just do an internal redirect:
RewriteRule ^/it/cartelle/(.*)$ /it/valigeria/cartelle/$1 [NC,L]

Redirect 301 & Mod Rewrite Issues

I have a online store that, for example, is located here: hxxp://domain.com/store/
Within the store directory's .htaccess file I have this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /store/
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/store/$1 [R=301]
RewriteRule ^/?$ directory.php
RewriteRule ^search/?$ search.php
RewriteRule ^category/([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]
It works great!
My issue (problem) is that I now need to change the /store/ directory to /shop/ which seemed simple enough. However, I need to setup proper 301 redirects so that I don't loose any SE rankings that I may have.
What is the best way to setup 301 redirects in this situation?
The only solution I have found is to setup a redirect 301 for each category, product etc. in the store. Like so, for example.
Redirect 301 /store/category/sample-widgets/ hxxp://www.domain.com/shop/category/sample-widgets/
This works and does what I need it to, but... the URL in the address bar displays like so: hxxp://www.domain.com/shop/category/sample-widgets/?CategoryID=sample-widgets
I can not figure out why or how to remove the query string.
Please help. Thanks.
You can handle the 301 errors by using a PHP script to handle the redirects.
In your .htaccess file you would add this rule:
Redirect 301 /error301.php
Create the file error301.php:
<?php
$redirects = array('/path/to/old/page/' => '/path/to/new/page/',
'/store/category/sample-widgets/' => '/shop/category/sample-widgets/');
if (array_key_exists($_SERVER['REQUEST_URI'], $redirects))
{
$dest = 'http://'.$_SERVER['HTTP_HOST'].$redirects[$_SERVER['REQUEST_URI']];
header("Location: {$dest}", TRUE, 301); // 301 Moved Permanently
}
You could use a simple Redirect directive like:
Redirect 301 /store/ /shop/
Or, if you want to use mod_rewrite as well, you would need to change your current rules as you can not use the base URL /store/ anymore:
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^store/?([^/].*)?$ /shop/$1 [L,R=301]
RewriteRule ^shop/?$ directory.php
RewriteRule ^shop/search/?$ shop/search.php
RewriteRule ^shop/category/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^shop/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]

Resources