301 correct way to redirect a directory - .htaccess

I was wondering what would be the correct way to do a redirect like this:
Redirect 301 /about/$ http://domain.com/new-about/
Redirect 301 /about$ http://domain.com/new-about/
Redirect 301 /about/me http://domain.com/new-about/
That's how I currently do it and it works but I believe there should be a better way?
I have 2 pages which are rewrited, one page is a subpage of the other page /about/me/, now both pages should redirect to the new page new-about

You should either use RedirectMatch or use mod_rewrite to perform regular expression matching. This should work and would be the shortest and best performing solution:
RedirectMatch 301 ^/about /new-about/
Domain and protocol are optional - leaving them out like this will keep them as they were, so this is future proof.

Been a while since I asked this question and I am now more comfortable with regular expressions.
The way I would do this now is:
Redirect 301 ^/about http://domain.com/new-about/
A tool which helped me a lot understanding regular expressions and which I would advice anyone trying to understand them is Rubular: http://rubular.com/

Related

Problem with redirection of shop subcategories in .htaccess

I couldn't find an answer, how to redirect certain subcategories...
I've old shop with paths like that:
shop.com/old_cat1/old_cat2/old_cat3
What I want, is to redirect it like that:
shop.com/new_cat2
shop.com/new_cat3
When I'm trying to use this code:
Redirect 301 /old_cat1/old_cat2/ https://shop.com/new_cat2
Redirect 301 /old_cat1/old_cat2/old_cat3 https://shop.com/new_cat3
redirection from old_cat2 to new_cat2 is working, but redirection to new_cat3 is sending me to new_cat2 (higher level). What should I do? I was trying with RedirectMatch, but it gave me nothing.

htaccess redirect with wildcard and not recursive

I've consolidated about 20 old pages into one new page, and want to redirect web links going to those pages to the new page.
I started out listing each one in htaccess as a Redirect 301, but thought I might save processing time to do a wildcard string match instead. Unfortunately it failed, because I suspect the page I want to go to is also caught in the wildcard.
For example I want to redirect, www.mydomain.com/catalog/listname_oranges.php, listname_lemons.php, listname_figs.php etc to redirect to www.mydomain.com/catalog/listname_addons.php
So I tried this, which failed:
RedirectMatch 301 ^/catalog/listname_.*$ /catalog/listname_addons.php
How do I fix this so its not recursive?
You can use a negative lookahead in your regex:
RedirectMatch 301 ^/catalog/listname_(?!addons\.php).*$ /catalog/listname_addons.php
This way, the listname_addons.php file won't match the regex but everything else will.

.htaccess redirect partial url pattern

I am having issues with writing the proper redirect statement, and unfortunately can't rap my head around the syntax needed. Other questions have similar problems, but I can't figure out how to properly reuse the information in other posts.
I have a url: www.site.com/.../CORE_Testing_5010 this unique.
I need the page to redirect to www.site.com/core-phase
The ... could be multiple directories /a/b/c/d/CORE_Testing_5010 or just /a/CORE_Testing_5010
Right now, I have 310 redirects for most of the possible directory combinations, but that is inefficient.
Some guiedence and explanation would be helpful.
Try:
RedirectMatch 301 /CORE_Testing_5010$ /core-phase
The regex pattern ignored everything before the last /, so there can be anything in front of /CORE_Testing_5010.
If you have rewrite rules, and using mod_alias (the RedirectMatch) is causing conflicts, then you can stick with mod_rewrite:
RewriteRule /?CORE_Testing_5010$ /core-phase [L,R=301]
and it needs to be before any rules that do routing.

Why htaccess redirect being overruled?

I've got:
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
which works fine, unless followed by:
RedirectMatch 301 ^/blog(/)?(.*)$ http://www.new-site.com/blog/$2
I've tried all kinds of versions, including RewriteRule, but nothing has worked. How do I keep the first specific rule, and write an "everything else keeps its request uri and query string" rule?
Thanks
Alright, assuming these are the only two lines, what I see is this:
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
RedirectMatch 301 ^/blog(/)?(.*)$ http://www.new-site.com/blog/$2
These are basically saying the same thing, that is, on a match, permanently redirect all blog queries to the new site.
With the second one you're saying match from the beginning the string /blog with a possible slash, which you'll capture, and possibly more information, which you'll also capture, then just put all that information into blog/extra-picked-up-info. This may be part of the problem, or you may be able to get around it by reordering the directives, and seeing if the lower directive receives precedence.
RedirectMatch 301 /blog(?:/\?)?(.*)?$ http://www.new-site.com/blog/$1
Redirect 301 /blog/?p=1 http://www.new-site.com/blog/2000/10/myslug/
Otherwise, you're going to need to reexamine your URIs, and find something more uniquely identifying.

how to use htaccess to chance the URL...?

I googled and looked on stackover flow but i failed to really understand the answers
I;m not trying to REDIRECT but CHANGE the WAY the URLS LOOK
I want to change a these into the new urls
www.site.com/abc.php to www.site.com/
(my index page currently has my login page so I can't use the index.php)
www.site.com/abc.php#123.php to www.site.com/123.php
UPDATE:
Ok, how can I do this then
www.site.com/abc.php to www.site.com/abc/
so that when a hash link is present it looks like
www.site.com/abc/#123.pho
or if possible
www.site.com/abc/#123/
In addition to the other answer on redirects, if you want more advanced forwarding you can use the Apache RewriteEngine module.
You can then use regexps, which may include subpatterns.
Example from my site, I created the patterns after I've imported everything from blogger to wordpress. Whenever someone visits an URL like http://www.twistedmind.nu/2006_03_01_archive.html he'd be redirected to http://twistedmind.nu/2006/03
RewriteEngine on
RedirectMatch 301 (([0-9]*)_([0-9]*)_([0-9])(.)(.html))$ http://twistedmind.nu/$2/$3/
RedirectMatch 301 (([0-9])(.)(.html))$ http://twistedmind.nu/$1
Based on the other answer, you can't match on everything that's after the hash tag # though.
Other example (added after comment):
RedirectMatch 301 (.*).php$ http://www.mysite.com/$1
This should strip the .php extension from all links, the new link (withouth .php) should exist.
You can use mod_rewrite if you want to create 'virtual' urls that redirect something like mysample to mysample.php. See http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for an explanation.
You can use an .htaccess only for the first url.
Redirect /abc.php http://www.site.com/
The second url cannot be redirected with the .htaccess.
You can use javascript for that:
<script type="text/javascript">
if (location.hash == "123")
location.href = location.hash+".php";
</script>

Resources