Forum Moderators: phranque

Message Too Old, No Replies

Rewrite "example/*.*" to "example/"

redirect everyone looking for files within a subfolder to the subfolder dir

         

chandldj

4:02 am on Dec 7, 2009 (gmt 0)

10+ Year Member



Trying to redirect "example/*.*" to "example/" for example:

www.xyz.com/example/blah/ should redirect to www.xyz.com/example/

I tried the following:

RedirectMatch 301 ^/example/.+$ [xyz.com...]

but site hangs when trying to access www.xyz.com/example/

Thank you in advance for your help!

g1smd

8:09 am on Dec 7, 2009 (gmt 0)

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



You have created an infinite loop as the new request again matches the pattern.

Is /example/ a real folder on the site, or is there a rewrite to an internal file involved to serve content.

If the latter, make the content URL

www.example.com/exampl[b]e[/b]
and it will not rematch the pattern.

In any case, if you were to use RewriteRule and not RedirectMatch you can have a rule which will not loop.

chandldj

1:29 pm on Dec 7, 2009 (gmt 0)

10+ Year Member



Thank you for the input. Each of 8 subdirectories on my site has 10-20 subfolders that I now need to eliminate and redirect to the subdirectory root. I'm trying to do this the simplest way possible.

All pages on my site are formatted as such: www.xyz.com/example/

I need to move:

www.xyz.com/example/1/
www.xyz.com/example/1/1/
www.xyz.com/example/2/
www.xyz.com/example/2/1/1/

ALL to www.xyz.com/example/

I'm pretty sure I have a rewrite that changes www.xyz.com/example to www.xyz.com/example/

Is there any way I can write a one line redirect to cover all of these situations?

Thank you!

jdMorgan

3:58 pm on Dec 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



/example/1/<whatever> --> www.example.com/example/<whatever>
/example/1/1/<whatever> --> www.example.com/example/<whatever>
/example/2/<whatever> --> www.example.com/example/<whatever>
/example/2/1/1/ <whatever> --> www.example.com/example/<whatever>

RedirectMatch 301 ^/example/([^/]+/)+(.*)$ http://www.example.com/example/$2

If you do not wish to retain the <whatever> -- a filename, for example, then delete the $2

Jim

chandldj

5:06 pm on Dec 7, 2009 (gmt 0)

10+ Year Member



Thank you so much for your help!

That works great, but there is one more issue that it doesn't cover.

I would like to also cover the possibility that someone writes:

www.xyz.com/example/a

If they don't put the 3rd slash, the redirect doesn't work.

If it cover that possibility as well, then I would be covered.

Thanks!

jdMorgan

5:09 pm on Dec 7, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm sorry, but we need to know what the code you tested looks like, since there was an 'option' in what I posted, and the option tested affects the answer to your new question.

Jim

chandldj

5:30 pm on Dec 7, 2009 (gmt 0)

10+ Year Member



In that case, try:

RedirectMatch 301 ^/cow-livestock-mats/. http://www.examplecom/cow-livestock-mats/

Be aware that the un-escaped period is a regex token for "any character." That is, at least one character is required in that position to invoke the redirect.

Jim

[edited by: jdMorgan at 8:26 pm (utc) on Dec. 7, 2009]

chandldj

2:07 am on Dec 8, 2009 (gmt 0)

10+ Year Member



Unfortunately, that resulted in an infinite loop as well. I will stick to your original answer since that covers all of the urls that I wanted anyway.

Thank you again for all your help!