Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Help with mod rewrite regular expression

Need to include dot as part of the condition



8:08 am on Dec 16, 2006 (gmt 0)

5+ Year Member

I have the following rewrite rule setup in my site

RewriteRule ^([^.]+)/?$ /search.php?l=$1 [L]

which will redirect urls like [mysite...] to [mysite...]

But now the problem I face is that the above rule breaks when there is a dot(.) in the url.

I tried to change the rule to

RewriteRule ^(.+)/?$ /search.php?l=$1 [L]

But is not working and gave me an internal server error.

So can any one help me with the rule so that it also includes url with a dot (.) in them.



9:04 am on Dec 16, 2006 (gmt 0)

5+ Year Member

RewriteRule ^([^.]+)/?$ /search.php?l=$1 [L]

I'm a bit confused. How does [^.]+ help you? Maybe someone else can help me understand. To me, that says anything except any character. Odd.

Anyway, can you just get away with something simple?

RewriteRule ^([A-Z0-9\.\-]+)/?$ /search.php?l=$1 [NC,L]


9:48 am on Dec 16, 2006 (gmt 0)

5+ Year Member

I am not an expert in mod_rewrite but this is how itís been working for me close to 2 years now.

Here dot (.) matches only the literal (.) character and is not matching Ďany character expect the new line characterí.

I want all the characters after the domain name to be passed to the query string (even special characters like $,#,^ etc) so I cannot use the rule

RewriteRule ^([A-Z0-9\.\-]+)/?$ /search.php?l=$1 [NC,L]

But anyways thanks for replying.


11:42 am on Dec 16, 2006 (gmt 0)

5+ Year Member

This should work

RewriteRule ^(.*)$ search.php?l=$1 [L]



11:57 am on Dec 16, 2006 (gmt 0)

5+ Year Member

Hi RazvanG,

Thanks for replying.

But when I try this I am getting internal server error with error code 500



2:47 pm on Dec 16, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

The regex token and escaping rules change slightly within [] character groups.

So [^.]+ means, "one or more characters not equal to a dot/period/full stop."

However, if you want to "allow" dots in the requested URL-path, and you change the rule pattern to this:

RewriteRule ^(.*)$ search.php?l=$1 [L]

Then a different problem arises: There is nothing to stop mod_rewrite from rewriting "search.php" to "search.php?l=search.php" -- or from doing so multiple times in an 'infinite' loop.

This problem can be corrected by adding a RewriteCond -- in this case:

RewriteCond $1 !^search\.php$
RewriteRule (.*) search.php?l=$1 [L]

Whenever you get a server error, check your server error log. Along with the server access log data, the information in the error log is often very useful.



3:06 pm on Dec 16, 2006 (gmt 0)

5+ Year Member

Hi Jim,

Works like a charm now.
Thanks for the rule and also for clearing up the confusion regarding [^.]+
You are a real genius! :-)


8:39 pm on Dec 17, 2006 (gmt 0)

5+ Year Member

glad it finally worked due to the experience of the community ;)

Featured Threads

Hot Threads This Week

Hot Threads This Month