1

Yes, I have read other similar questions on SO about this, which is how I came up with this possible solution:

# Turn on the mod_rewrite engine
RewriteEngine On

# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_FILENAME}.php -f

# AND the request is not for a directory
RewriteCond %{REQUEST_URI} !/$

# Redirect to the php script with the requested filename
RewriteRule (.*) $1\.php [L]

This works by rewriting a request for, for example: /about-us to about-us.php but it doesn't work if you go to /about-us/.

I tried the below but it didn't work:

# Redirect to the php script with the requested filename
RewriteRule (.*)/? $1\.php [L]

Does it have anything to do with the second RewriteCond?

Basically I am wanting it to fail (if possible) if the request is for a real file OR directory, but if neither exist than it should rewrite to a PHP script whether the request has an ending slash or not.

Can this be done?

1 Answer 1

1

Have your rule this way:

When .htaccess and .php files are in a sub directory then use:

RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}/work/Client_Name/public_html/$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]

When .htaccess and .php files are directly in root of site then use:

RewriteEngine On 

RewriteCond %{DOCUMENT_ROOT}$1\.php -f [NC] 
RewriteRule ^(.+?)/?$ $1.php [L]
Sign up to request clarification or add additional context in comments.

6 Comments

It might be because I am working out of a subdirectory on my dev machine though?
Hmmm the updated rule worked without the ending slash, but not with it.
It's 3 directories off the root directory in a dir called public_html. Can we set the RewriteBase perhaps?
Yes that's why I wanted to know your sub-dir path relative from public_html
That would be ../../../ to the '''web root''' from public_html. But the directory public_html serves as the root for this particular site. The files then would be at /public_html/about-us.php and requested via public_html/about-us/.
|

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.