homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Help with mod rewrite regular expression
Need to include dot as part of the condition

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

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)

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)

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)

This should work

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



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

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)

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)

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)

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

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved