Convertor
Convertor

Reputation: 171

.htaccess rewrite help, remove .php from URL

I am not facing another problem with my mod_rewrite, first, time is my .htaccess file:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L] <- This line doesn't work
RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [L,QSA]

All lines are working, beside the RewriteRule ^([^.]+)$ $1.php [NC,L] and when I use this line, all other rules gets interrupted.

What am I doing wrong?

Upvotes: 1

Views: 369

Answers (2)

rernesto
rernesto

Reputation: 552

Try something like

#Preventing loop 
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]

RewriteCond %{REQUEST_URI} !^.+\.php.*$
RewriteRule .* - [L]

Put this after RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]

Upvotes: 1

anubhava
anubhava

Reputation: 785098

You need to use L flag in your problematic rule.

For removing the .php extension this is better rule:

Options +FollowSymlinks -MultiViews

RewriteEngine on

## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^news/([0-9]+)/?$ /news?id=$1 [L,QSA]
RewriteRule ^contact/([0-9]+)/?$ /contact?do=$1 [L,QSA]
RewriteRule ^account/([a-zA-Z]+)/?$ /account?action=$1 [L,QSA]
RewriteRule ^admin/([a-zA-Z]+)/?$ /admin?action=$1 [L,QSA]
RewriteRule ^login/([a-zA-Z]+)/?$ /login?action=$1 [L,QSA]
RewriteRule ^register/([a-zA-Z]+)/?$ /register?action=$1 [L,QSA]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]

Upvotes: 2

Related Questions