Forum Moderators: phranque

Message Too Old, No Replies

Rewrite to remove folder with combination of wildcards

Rewrite rule for multiple folders with wildcards

         

Fleury75

6:49 pm on May 5, 2010 (gmt 0)

10+ Year Member



Hi all,

I've been trying to solve an htaccess problem for some time, but no one seems to be able to help fully fix the issue. I cant seem to find a similar issue here in the posts, so here it is in a nutshell:

I need to completely remove a folder from my URL for thousands of pages.The folder that needs to be removed varies slightly. It is either:

/womens-#/ (where # is a 1-3 digit number)
/mens-#/ (where # is a 1-3 digit number)

All Im trying to do is remove the folder, for example:

www.URL.com/folder1/folder2/mens-222/example-product

redirected to:

www.URL.com/folder1/folder2/example-product


One programmer told me to do this all in one rule (instead of creating separate rules for mens and womens. This is what I came up with but something is obviously not working:

RewriteCond %{REQUEST_URI} ^(.*)/mens|womens)-(.*?)/(.*)$
RewriteRule ^(.*)/(mens|womens)-(.*?)/(.*)$ http://%{HTTP POST}/$1/$4 [L,R=301]

I know this is close, but it is not working. Can anyone point me in the right direction?

[edited by: jdMorgan at 3:05 am (utc) on May 6, 2010]
[edit reason] de-linked redirect URL [/edit]

g1smd

7:10 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



To remove the folder from the URL, you need to link to a URL that does not contain that element. It is links that 'define' URLs, so link from the pages of your site to the exact URL format that you want users to see and use.

The redirect is there not to "fix" the URL that usual visitors see, but to tell any agents still asking for the old URL to update their index to include the new URL instead.

Any rule with multiple (.*) constructs is going to fail. The initial (.*) pattern is likely better replaced with (([^/]+/)*) or similar. Test for "up to three digits" using ([0-9]{1,3}) or similar.

jdMorgan

3:15 am on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And note that the variable in the redirect target should be "%{HTTP_HOST}", not "%{HTTP POST}". In fact, unless this code needs to support multiple hostnames ('domains'), you'd might as well just hardcode the canonical hostname in the rule.

Assuming that you've already corrected your on-page links then, and are relying on this rule only to speed up the updating of old URLs in search engine listings, all you should need is one line:

RewriteRule ^(([^/]+/)*)(wo)?mens-[0-9]{1,3}/(.*)$ http://%{HTTP_HOST}/$1$4 [R=301,L]

See the resources cited in our Apache Forum Charter, and the examples and tutorials in our Library for more information. Links to these are at the top of this page.

Jim

Fleury75

4:26 pm on May 6, 2010 (gmt 0)

10+ Year Member



@ G1 + jdMorgan

Thanks guys,

This worked, I was coming at it the wrong way. HTTP_HOST did have underscore, it just copy pasted as a URL w/UL.

As an SEO I'm not the best at this but Im trying to learn!

Thx again.

g1smd

5:57 pm on May 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's see your final code, in case there are other improvements that can be made.

Fleury75

6:33 pm on May 7, 2010 (gmt 0)

10+ Year Member



Will do once finished and added a few things. Thx again

Fleury75

5:59 pm on May 10, 2010 (gmt 0)

10+ Year Member



In the end, all I nbeeded to do was use the code jd showed, and then add another amended line for the /mens/ and /womens/ folders with no digits (and no dash):

RewriteRule ^(([^/]+/)*)(wo)?mens/(.*)$ http://%{HTTP_HOST}/$1$4 [R=301,L]
RewriteRule ^(([^/]+/)*)(wo)?mens-[0-9]{1,3}/(.*)$ http://%{HTTP_HOST}/$1$4 [R=301,L]

This worked perfect. Thx again for the help.

[edited by: jdMorgan at 6:28 pm (utc) on May 10, 2010]
[edit reason] de-linked. [/edit]

jdMorgan

6:32 pm on May 10, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can easily combine them into one line:

RewriteRule ^(([^/]+/)*)(wo)?mens(-[0-9]{1,3})?/(.*)$ http://%{HTTP_HOST}/$1$5 [R=301,L]

Jim

Fleury75

6:38 pm on May 10, 2010 (gmt 0)

10+ Year Member



Hey this is great. I thought there must be a way but I have much to learn....

Thanks for de linking and the help.