2

I have this working htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/([0-9]+)/?$ test.php?id=$1 [L]

my question for this if I have that rule on my htaccess how can I load php file that have no .php extension.

I add this to my htacess

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

It works fine http://localhost/myprojectfolder/account but this will not work anymore http://localhost/myprojectfolder/1 how can I make them both working ?

Thank you in advance.

1
  • black milk? try to understand what you edit and what it means Commented Jun 26, 2015 at 17:12

2 Answers 2

3

Try adding another condition to your php extension rule:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

that ensures that when you add the .php extension, it actually points to a file that exists.

Sign up to request clarification or add additional context in comments.

1 Comment

Or much better, just say: RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^([^\.]+)$ $1.php [NC,L]
2

If you are using this URL http://localhost/myprojectfolder/1 as in your other question. Then test shouldn't even be in the first rule. Then you can just make sure the file exists.

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ test.php?id=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Comments

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.