1

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?

2 Answers 2

2

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]
Sign up to request clarification or add additional context in comments.

7 Comments

Doesn't work, it wont show home page, some pages do work, and the pages where I want to remove .php doesn't work
www.domain.com doesn't work, www.domain.com/about does work, www.domain.com/api/file doesn't work, www.domain.com/api/file.php does work, the 2 first URLs are routed thru index, as you see in the .htaccess file :)
it should show home.phtml, the about link is also routing to about.phtml which works
1. You wanted to hide .php not .phtml. 2. Where is the code that says show home.phtml for www.domain.com?
when you visit domain.com then the index routes into /templates/home.phtml, if I go to domain.com/about, then the rules makes it domain.com/index.php?page=about, it all routed thru PHP in index, .phtml is removed because of the routing, and the rule: RewriteRule ^([\w\d~%.:_\-]+)$ index.php?page=$1 [L,QSA] does so it doesn't need to say index.php?page=about, but just .domain.com/about, so there is no .phtml, and on the site, there is either no .php, but I have an API service on the site to, and which ain't included in the routing, so I need to put api/file.php and I want to remove the .php
|
1

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]

2 Comments

when I do that, then the page is going in an never ending loop :/
See above, i fix the tip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.