lowercasename
lowercasename

Reputation: 281

mod_rewrite and the regex OR condition

Does the OR condition work in mod_rewrite? My rule is throwing an internal server error:

RewriteRule ^(Bob|Sam|Arnold|Brian).htm$ http://newdomain.net/$1 [R=301, L]

Alternatively, is there a better rewriting system which can load a list of possible page names to rewrite? Or perhaps an inverse way to do this - regex to select which pages NOT to rewrite? (Say, not index, abbreviations, or contact).

Upvotes: 2

Views: 5199

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

You're probably getting the 500 server error because you have a space before the L flag. Apache isn't all that clever about parsing directives and their parameters, so when apache see's a space, it assumes the beginning/end of parameters. So [R=301, and L] end up being interpreted as 2 different parameters, and they're not valid separately like that.

The "OR" functionality is fine.

RewriteRule ^(Bob|Sam|Arnold|Brian)\.htm$ http://newdomain.net/$1 [R=301,L]

You can do negative matches using a ?! type of group:

RewriteRule ^(?!index|abbreviations|contact)(.*)\.htm$ http://newdomain.net/$1 [R=301,L]

Upvotes: 3

Yes Barry
Yes Barry

Reputation: 9876

Here is how I would handle the OR. You'll need to separate them into RewriteCond.

RewriteEngine On
RewriteCond %{REQUEST_URI} ^(Bob|Sam|Arnold|Brian).htm$ [OR]
# Another RewriteCond would go here as the "OR" like:
RewriteCond %{REQUEST_URI} !^(index|abbreviations|contact) # ! = "not"
RewriteRule ^(.*)$ http://newdomain.net/$1 [R=301, L]

As far as being able to "load a list of possible page names to rewrite," I don't know of any way to do that other than listing them manually with mod_rewrite. I know nginx, for example, supposedly has more in depth features involving that.

Upvotes: 1

Related Questions