mod_rewrite not recognizing files - php

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>

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?

Try removing the 'L' from the first rewrite rule, this tells Apache to stop processing further rules if a match is found.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/?$ /city_name/index.php?page=$1 [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>

Related

Apache - using .htaccess redirect to root folder

I have a problem with the .htaccess file. I have it configured in this way.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/$ index.php?controller=$1 [L]
Every time I access http://localhost/mvc3/contacto/ I'm accessing http://localhost/mvc3/index.php?controller=contacto, up there it is correct but if the friendly URL I remove the final character / (http://localhost/mvc3/contacto) it returns me to the root page (wampserver server).
Here is explained in video of the problem, what would be happening?
Video ▶️ http://recordit.co/FfXHww1xCv
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?controller=$1 [L]
Remove slash from the rewrite rule and it should work.
may below changes in .htaccess will be helpful for you
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ index.php?controller=$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
</IfModule>

.htaccess Rewrite URL same page

I want to rewrite the following URLs:
localhost/beta/coupons.php?cat=promotions point to ---> localhost/beta/coupons/promotions
localhost/beta/coupons.php?city=ny point to ---> localhost/beta/coupons/ny
I run this code in .htaccess
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /beta/
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^coupons/([^/]*)$ coupons.php?cat=$1 [L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^coupons/([^/]*)$ coupons.php?city=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^coupons/([^/]+)/([^/]+)?$ coupons.php?cat=$1&city=$2 [L]
</IfModule>
The code only runs the first re writing, the second does not work.
Thank you very much for your time.

.htaccess RewriteCond %{REQUEST_FILENAME} !-d not working

.htaccess code is,
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1 [NC,L] #
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] #
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] #
I'm trying to access http://example.com/directory/ which is already exist in server but i can't access it. There is no any error occuring but, if there is a request for already existant directory, rules after RewriteCond %{REQUEST_FILENAME} !-d should not be applied.
But in this case one of the 3 rules are applying.
RewriteCond %{REQUEST_FILENAME} !-d is seems not working.
RewriteCond only applies to the next RewriteRule, not to all of them. You should then duplicate them like so :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1 [NC,L] #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L] #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L] #
An alternative solution consists in using the S flag (skip) and invert the RewriteCond : Apache doc for S flag or examples (article in French, but the htaccess snippet don't need translation :) )
EDIT with another solution (not tested) :
# if it's a file or a folder
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .* - [S=3] # then skip the next 3 rules
RewriteRule ^p/([A-Za-z0-9-_]+)/?$ redirect.php?url=$1 [NC,L]
RewriteRule ^[A-Za-z0-9-_]+/([A-Za-z0-9-_]+)/?$ product.php?seo_id=$1 [NC,L]
RewriteRule ^([A-Za-z0-9-_]+)/?$ catalog.php?seo_id=$1 [NC,L]

Apache mod_rewrite problems

I have a project with the following in the .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L,NC,QSA]
I have two files in a directory test.txt & test.php
I can access /path/to/folder/test.txt but not /path/to/folder/text.php.
What am I missing? This configuration did work before on a different server.
Try this...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.php [L,NC,QSA]
</IfModule>

htaccess rewrite condition not working

I am working on URL of my website. I have just rewrite the URL From
http://www.website.com/index.php?url=some-text
to
http://www.website.com/some-text
For this I'm using following code of .htaccess
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Now, I want to add another variable page number in URL.
http://www.website.com/index.php?url=some-text&page=1
I want to rewrite it like
http://www.website.com/some-text/1/
AND
http://www.website.com/some-text/1
I have tried following code but some reason it is not working, it showing me server error.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*)$ index.php?url=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/(.*)/$ index.php?url=$1&page=$2 [L,QSA]
You don't need 2 new rules. Just one with optional / will be enough. Your complete code:
Options +SymLinksIfOwnerMatch -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/?$ index.php?url=$1 [L,QSA]
Another problem in your new code is that RewriteCond are not getting applied for last RewriteRule since these are applicable to very next RewriteRule only.

Categories