htaccess redirect catchall - apache

I need to redirect a directory, and all subdirectories and files in that directory, to the same location (root). So anyone who tries to visit /old, /old/folder, /old/other-folder/xy/page.php, or anywhere else within the 'old' folder, should be redirected to the root domain.
So far, I have this:
Redirect 301 ^/old/.*$ /
Is this the best way of doing it, or would it be better to use (.*) instead of .*? What is the difference between the two?
Or - should I use a RewriteRule instead of a Redirect like above? If so, why?
Thank you!

The Redirect directive doesn't use regular expressions. It connects 2 path nodes together, which isn't exactly what you want. You can try using the RedirectMatch directive instead:
RedirectMatch 301 ^/old/ /

You can try this
RewriteEngine On
RewriteBase /
RewriteRule ^old/?(.*) /$1 [R=301,NC,L]
If you just want to redirect every old page to homepage/root(I'm not sure exactly what you want) than you can replace last rewriterule with
RewriteRule ^old/.* / [R=301,NC,L]

Related

.htaccess 301 permanent redirect rule

I have this url structure right now.
http://example.com/weather/in-city_name
which I want to permanently redirect to
http://example.com/city_name/weather
Here is what I'm writing to the .htaccess file using this reference
RedirectMatch ^/weather/(.*)$ http://example.com/$1/weather
But this doesn't work.
In my condition city_name is dynamic and manual entry is not possible.
Any suggestion about how to achieve the desire result would be great.
You can use this .htaccess:
RewriteEngine on
RewriteRule ^weather/in-(.+)/?$ /$1/weather [R=301,NC,L]

Create a 301 redirect excluding a subdirectory [duplicate]

I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.

Mod_Rewrite assistance for a noob please

I want to rewrite all URLs of the form:
http://www.site.com/push20/dir1/dir2/afile.html
to
http://www.site.com/dir1/dir2/afile.html
I.e. lose the push20 bit
I've tried with:
RewriteEngine On
RewriteRule ^push20/(.*) /$1 [R]
but its not happening.
Any suggestion?
Won't you need a leading / before push20?
i.e:
RewriteRule ^/push20/(.*) /$1 [R]
Unless I'm missing something, you can do this with a standard Redirect directive, something along these lines:
Redirect /push20 /
That should redirect everything under /push20 to the root of your site -- basically it strips out push20 and redirects.

Redirect site with .htaccess but exclude one folder

I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.

Apache redirect

I would like to redirect a URL using RedirectMatch within Apache eg,
/test/one/?? redirect to /test/two/??
where the ?? represents any string that follows
The redirect i'm using below does a straight redirect but doesnt match any string after...
RedirectMatch Permanent ^/test/one?$ /test/two/
Thanks alot
RewriteEngine ON
RewriteBase /
RewriteRule ^/test/one/(.+)$ /test/two/$1
if that does not work, change ^/test/one into ^test/one
make sure mod_rewrite is enabled
You can use mod_rewrite for this:
RewriteEngine On
RewriteBase /
RewriteRule ^/test/one/(.*) /test/two/$1 [L,R=301]
The R flag redirects the page rather than internally rewriting the URI. 301 is the HTTP status code for "Permanently Moved" - if you'd rather use another, you can change it to one of these.