homepage Welcome to WebmasterWorld Guest from 54.235.227.60
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
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
Sudar




msg:3190322
 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.

Thanks
Sudar

 

equalm




msg:3190338
 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]

Sudar




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

Equalm,
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.

RazvanG




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

This should work

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

cheers

Sudar




msg:3190403
 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

Thanks,
Sudar

jdMorgan




msg:3190498
 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.

Jim

Sudar




msg:3190515
 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! :-)

RazvanG




msg:3191281
 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