Sampson
Sampson

Reputation: 268462

mod_rewrite multiple directories to a php file

RewriteEngine on
RewriteRule ^/(dir1|dir2|dir3)/(.*)$ /targetfile.php [R,L]

http://www.somesite.com/dir1 -> http://www.somesite.com/targetfile.php
http://www.somesite.com/dir2 -> http://www.somesite.com/targetfile.php
http://www.somesite.com/dir3 -> http://www.somesite.com/targetfile.php

From what I've seen online, this should work. Unfortunately, it wont. Any insight?

Upvotes: 1

Views: 787

Answers (3)

Antti Huima
Antti Huima

Reputation: 25542

I think the problem is that the regular expression mandates a slash after the directory name (e.g. /dir1/), but in the example the last slash is omitted (http://www.somesite.com/dir1 does not have trailing slash).

I think you could try just with

RewriteRule ^/(dir[1-3]) /targetfile.php [R,L]

Upvotes: 0

Brian D.
Brian D.

Reputation: 21

I don't believe the forward slashes are necessary, unless you want to restrict it to requiring the trailing slash after "dir1."

Try: RewriteRule ^(dir1|dir2|dir3)$ targetfile.php [QSA,L]

Upvotes: 1

Gumbo
Gumbo

Reputation: 655755

If you want to use this in a .htaccess file, remove the leading slash from the pattern. And to match only full path segments, you have to alter the expression a little bit.

So try this:

RewriteEngine on
RewriteRule ^(dir1|dir2|dir3)(/|$) targetfile.php [R,L]

Upvotes: 1

Related Questions