oliverjrose99
oliverjrose99

Reputation: 47

.htaccess not working with some PHP files

I have a .htaccess file that wont find some php files but will find others. Example,

RewriteRule ^test$ test.php

will give a 404 not found but

RewriteRule ^custom$ test.php

will work.

Its the same for a rule that will add .php to the end of the URL. Any ideas?

Full file

RewriteEngine On
RewriteRule ^test$ test.php #doesn't work
RewriteRule ^custom$ test.php #works

full directory(ls -a)

.  ..  .htaccess  index.html  test.php

Thanks.

Upvotes: 0

Views: 286

Answers (3)

Abdul Rehman
Abdul Rehman

Reputation: 1794

When there exist a file/folder in your root directory with rule name, it wont work as desired. Because entering http://127.0.0.1/test will change it to http://127.0.0.1/test/ directory by default. but not with http://127.0.0.1/custom so add a trailing slash to your rule.

RewriteEngine On
RewriteRule ^test/$ test.php
RewriteRule ^custom$ test.php

Upvotes: 1

Panama Jack
Panama Jack

Reputation: 24448

Make sure multiviews is OFF so that it's not causing any funny business trying to match files. Also always use the [L] so that it stops processing rules when a rule is met. That can also cause issues down the line by continuing to run other rules. It should not matter if you have a trailing slash or not. For good measure you can check with conditions too so that if it's not a real file or not a real directory it will process the rule and you won't get a 404.

Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test/?$ test.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^custom/?$ test.php [L]

Upvotes: 3

Xander Luciano
Xander Luciano

Reputation: 3893

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1.php [L,R=301]

This is what I use on my website to add trailing slashes, I modified it to add .php instead. Is this what you are looking for?

or this should work also

RewriteRule ^test test.php [L,R=301]
RewriteRule ^custom test.php [L,R=301]

Upvotes: 0

Related Questions