htaccess wildcard redirect to parent folder - .htaccess

a simple request I'm sure but can't for the life of me find an answer.
I would like the following...
http://example.co.uk/menu/item1, http://example.co.uk/menu/item2, http://example.co.uk/menu/item3 etc.
To redirect to http://example.co.uk/menu/.
Currently I am using the rule below but am getting a redirect loop on /menu/.
RedirectMatch 302 ^/menu/.*$ http://example.co.uk/menu/
How do I create a rule that redirects only what I require and leaves /menu/ accessible?
Thanks.

Tweak your regex a little by matching 1 or more characters after /menu/:
RewriteRule ^menu/(.+)$ /menu/ [L,R=301]

Related

How can i 301 redirect a subfolder and everything after it?

I am trying to redirect a subfolder as well as anything after it to the home page.
For example:
example.com/subfolder/extra-stuff > example.com
The extra-stuff is constantly changing and auto generated, so I want the redirect to remove that as well.
I am using:
Redirect 301 /subfolder(.*) http://www.example.com
However, this will result in http://www.example.com/extra-stuff.
Is there a way I can say if /subfolder(and anything else after subfolder) redirect to home?
Thanks for any suggestions!
The Redirect directive uses simple prefix-matching and everything after the match is copied onto the end of the target URL (which is what you are seeing here). However, the Redirect directive also does not support regex syntax, so a "pattern" like (.*) on the end will actually match the literal characters (, ., * and ) - which shouldn't have worked in your example?!
You'll need to use RedirectMatch instead (also part of mod_alias), which does use regex, and is not prefix matching.
For example:
RedirectMatch 301 ^/subfolder http://www.example.com/
Any request that starts /subfolder will be redirected to http://www.example.com/ exactly.
You'll need to clear your browser catch before testing.
You tagged your question "Magento" (which is probably using mod_rewrite). You should note, however, if you are already using mod_rewrite for rewrites/redirects then you should probably be using mod_rewrite instead of mod_alias to do this redirect, since you can potentially get conflicts.
For example, the equivalent mod_rewrite directive would be:
RewriteRule ^subfolder http://www.example.com/ [R=301,L]
Note there is no slash prefix on the RewriteRule pattern. This would need to go near the top of your .htaccess file.

Static (seo friendly) to dynamic url rewrite

Here is my problem...
I have a to redirect a link like this:
site.com/virtualfolder/some-seo-friendly-keywork
to
site.com/folder/page.php?id=some-seo-friendly-keyword
In site.com there is no real folder "virtualfolder", it is virtual.
I have to take "some-seo-friendly-keyword" and use it as a query string
I think i have to firts match the folder "virtualfolder" and then capture the "some-seo-friendly-keyword", but how?
The some-seo-friendly-keyword is a string of characters and digits plus hypens so something like this below is realistic?
RewriteRule ^virtualfolder/([a-zA-Z0-9-])? folder/page.php?id=$1 [L]
I'm still strudying and trying mod_rewrite and it is like voodoo to me! :-/
Thank you very much for your help or your suggestions
Try with this code in htaccess:
Using the original url to show:
Rewriterule ^virtualfolder/([a-zA-Z0-9_-]+)$ folder/page.php?id=$1
Redirecting to end url using RedirectMatch (use the full URL in the second part):
RedirectMatch 301 ^/virtualfolder/([a-zA-Z0-9_-]+)$ http://www.site.com/folder/page.php?id=$1
Redirecting to end url using mod_rewrite (use the full URL in the second part):
Rewriterule ^virtualfolder/([a-zA-Z0-9_-]+)$ http://www.site.com/folder/page.php?id=$1 [R=301,L,NE]
More info here

How to 301 redirect pages "up" a page

I'm a newbie and I'm trying to figure out the proper 301 redirect for the following pages. I hope I'm being clear here :) In my .htaccess file, I want to redirect pages "up" one pages without having to do every page individually.
My original pages looked like the following:
www.doctors.com/skin/california/best-skin-doctors-california/
www.doctors.com/skin/california/best-skin-doctors-california/?page=1
www.doctors.com/skin/california/best-skin-doctors-california/?page=2
....etc. ....up to like /?page=33
and more categories and states, like:
www.doctors.com/heart/new-york/best-heart-doctors-new-york/
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=1
www.doctors.com/heart/new-york/best-heart-doctors-new-york/?page=2
...etc. .....again up to like /?page=24
I've since changed the page structure to eliminate the long URLs...like this:
www.doctors.com/skin/california/
www.doctors.com/skin/california/?page=1
www.doctors.com/skin/california/?page=2
etc.....and similarly....
www.doctors.com/heart/new-york/
www.doctors.com/heart/new-york/?page=1
www.doctors.com/heart/new-york/?page=2
etc.
How can I "bulk" redirect the original pages with the long URLs to the newer, shortened version in my .htaccess file? Thank you very much for your time and consideration!
Using mod_alias, you can simply add this in the .htaccess file in your document root:
RedirectMatch 301 ^/([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/
But if you need further restrictions on how the redirect works, you can use Apache's mod_rewrite module. Taking a look at the RewriteCond directive, you can impose conditions on a rule and put everything in .htaccess. The main rule will look very similar to mod_alias' RedirectMatch. Example:
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
In both cases, the query string (the page=3 part) is simply appended to the new target. Looking over the different things you can do with RewriteCond, say if you wanted to exclude this rule when requests are made for something like /images/ or /themes/:
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/themes/
RewriteRule ^([a-z\-]+)/([a-z\-]+)/[a-z\-]+/$ /$1/$2/ [R=301,L]
So, if the request doesn't start with /images/ and the request doesn't start with /themes/, then apply the rule. This example would make it so a request for http://host.com/themes/subSilver/magic-icons/ don't get redirected to http://host.com/themes/subSilver/.

.htaccess 301 Redirect Appending Query's to End of URL

I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?

.htaccess Redirect sub-folder

Hi I'm not a programmer by any stretch of the imagination and am trying to do a multi 301 redirect in my htaccess file based on the following:
So I have a ton of urls all with similar naming conventions - here is a sample of 2.
http://www.hollandsbrook.com/garrett-at-gold/
http://www.hollandsbrook.com/garrett-ace-250/
These urls need to redirect to:
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-at-gold/
http://www.hollandsbrook.com/garrett-metal-detectors/garrett-ace-250/
I could just redirect them 1 line at a time, but I'd like to use regex.
Here's what I was thinking so far but not working:
RewriteRule ^garrett-([a-z])/$ /garrett-metal-detectors/$1/ [R]
Basically i need to redirect any page right off the root that starts with "garrett-" to include the folder path of "garrett-metal-detectors".
Any thoughts would be MUCH appreciated. Many thanks in advance for your help.
if you want temprorary redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=302,L]
if you want permanent redirect use:
RewriteRule ^garrett\-([a-z0-9\-]+)/?$ /garrett-metal-detectors/garrett-$1/ [R=301,L]
I'm am not an expert on Regular Expressions, but looks like your reg ex may be a bit off...
try:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^((garrett)(-[a-z0-9]).*)/$ /metal-detectors/$1/ [R]
This is looking fro anything starting with "garrett" followed by any letter/number/hyphen combo.
Note: having "garett" in the destination part give you a loop of redirects, so you may have to choose a different word, or remove it all together...

Resources