Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite not recognizing files

My mod_rewrite is working excellent, but it's not recognizing anything after for example /calgary/

So, if I go to /calgary/login.php and it is only seeing the index.php page? It won't recognize the /login.php page?

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /city_name

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
    RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
    RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/?$2.php?page=$1 [L,QSA]
</IfModule>
like image 369
Jordie Avatar asked Aug 21 '26 04:08

Jordie


1 Answers

You need to swap those 2 rules:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /city_name

    # This used to be the 2nd rule
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
    RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
    # there used to be a "?" here, remove it ----v
    RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]

    # this used to be the first rule
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule (.*)/?$ /city_name/index.php?page=$1 [L,QSA]
</IfModule>

The less restrictive rule (the one that matches (.*)/?$) will match /calgary/login.php outright before the second set of rules gets to do its thing, which seems to be rewritten to /city_name/?login.php?page=calgary.

Is that really what you want? There are two ? in the target there. Maybe you only want /city_name/$2.php?page=$1?

like image 150
Jon Lin Avatar answered Aug 23 '26 18:08

Jon Lin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!