Forum Moderators: phranque

Message Too Old, No Replies

redirect subdirectory but not lower subdirectories

         

george93510

8:56 pm on Jun 13, 2014 (gmt 0)

10+ Year Member



I want to redirect any request for a specific subdirectory to a single page, but I don't want to redirect the lower subdirectories. I got as far as:
RedirectMatch 301 ^/store(.*)$ https://www.example.com/fc/store.php

So how do I limit it to that subdirectory only and not any underneath it?

phranque

9:31 pm on Jun 13, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you will need mod_rewrite for this instead of mod_alias directives - RewriteRule vs Redirect(Match).
use a RewriteCond to exclude subdirectories.

george93510

11:42 pm on Jun 13, 2014 (gmt 0)

10+ Year Member



Thanks. Can you give an example of a RewriteCond that selects the store directory and excludes its subdirectories?

lucy24

1:08 am on Jun 14, 2014 (gmt 0)

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



It depends on the URL format. What's inside /store/ ?

The very simplest form can be done in mod_alias (Redirect by that name); you'd just need to say RedirectMatch instead of Redirect.

^store/\w+\.html

\w is obviously the best-case scenario; it really means
[^/]
"anything other than a directory slash" and then replace
\.html
with whatever extension you actually do use.

If all your redirects are currently done with mod_alias you may be able to keep it that way. If you need a RewriteCond-- or if you already have rules using mod_rewrite-- you'll need to translate everything. By default, neither mod_alias nor mod_rewrite affects the query string, if any.

george93510

4:53 am on Jun 14, 2014 (gmt 0)

10+ Year Member



Store has a bunch of php programs. I tried the line below, based on your suggestion, but it doesn't seem to do anything at all:

RedirectMatch 301 ^store/\w+\.php https://www.example.com/fc/store.php

lucy24

9:49 am on Jun 14, 2014 (gmt 0)

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



RedirectMatch 301 ^/store

Did the original form with leading / slash work? (Working too well counts as working.) If so, keep the slash. A simple \w is enough if your filenames use only alphanumerics and lowlines; if you've got hyphens or worse* you'll need to go to [^/] instead.

Dang. Can't remember if mod_alias wants a leading slash. I haven't used it in ages.

Are your rules located in htaccess, in a <Directory> section, or loose in the config file? Do other rules work?


* "Worse" in an URL means miscellaneous punctuation. Legal but messy.

george93510

4:10 pm on Jun 14, 2014 (gmt 0)

10+ Year Member



Hi Lucy24 - The rule is loose in .htaccess. The rule I had in my first post worked, except it redirected the subdirectories below /store too, which meant we couldn't get to the admin area of the old store.

I tried your latest suggestion:
RedirectMatch 301 ^/store https://www.example.com/fc/store.php

and it redirects store and all lower subdiretories. I want to limit it just to store.

Regular expressions give me a headache.

lucy24

8:14 pm on Jun 14, 2014 (gmt 0)

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



Oops, sorry, you need to combine your original format-- with leading slash-- with the more narrowly constrained wording. So the pattern is

^/store/\w+\.php


assuming your filenames contain no hyphens. (Leading slash? Really? Coulda sworn they're not used-- but I did say it's been a while.)

loose in .htaccess

Well, that's redundant, because you can't use <Directory> sections in htaccess (or, for that matter, nested inside each other, worse luck, so I guess Apache is being consistent here). Technically you can put rules inside <Files> or <FilesMatch> envelopes, but that's a whole nother issue.

Regular expressions give me a headache.

At the beginning I was deathly afraid of Regular Expressions. This is right and proper, because you can seriously injure yourself with an incorrectly applied RegEx. But, like juggling chainsaws, you just have to know how to do it.