.htaccess rewrite question

hi.

i’m having some issues with a rewrite.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^mobile/(.+)$ /mobile.php?ff=$1 [L,QSA]
RewriteRule ^(.*)$ rewrite.php?ff=$1 [L,QSA]

please explain why www.domain.com/mobile/ is not redirecting to mobile.php

Thanks.

Peanuts

Because it’s expecting one or more characters after the mobile/. I’m betting www.domain.com/mobile/1 will redirect.

That makes sense … So now I need to figure out how to cope with that… I’ll post back after

Replace (.+) with (.*)

Tried that … Didn’t work… Let me test some more

ok both don’t work…

so when i do /mobile/1 it doesn’t redirect and when i change the + to an * it don’t work either :frowning:

i’m wondering if it’s because I’m on WAMP

I’m wondering about the “not” conditions

i.e. if it isn’t a directory and it isn’t a file then apply the rewrite rule

I’m also wondering how the “last” flag affects things here

got it working by doing this…

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule  ^/mobile/(.*)$ mobile.php?ff=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?ff=$1 [L,QSA]
1 Like

peanuts,

Great! You understand that a RewriteRule includes the preceding RewriteCond statements (which the prior code did not for the rewrite.php redirection).

Now, all you need to learn is that the :fire: EVERYTHING :fire: (or NOTHING) atom is probably the most dangerous and is certainly the most abused regex there is. Think about it: What if the request is empty? What if it contains a cross-site hack of your database? Are you thoroughly scrubbing this USER INPUT before your database query in mobile.php and rewrite.php?

Those (.*) you have in your code had me create a standard rant (when I was the TL for SitePoint’s hosting boards) :

[rant #1][indent]The use of “lazy regex,” specifically the :kaioken: EVERYTHING :kaioken: atom, (.*), and its close relatives, is the NUMBER ONE coding error of newbies BECAUSE it is “greedy.” Unless you provide an “exit” from your redirection, you will ALWAYS end up in a loop![/indent][/rant #1]

If you need to review a tutorial on proper regular expressions for use with mod_rewrite, I had also created a tutorial for members at http://dk.co.nz/seo. If you have questions, you know where to find me.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.