Andrew
Andrew

Reputation: 2154

.htaccess Redirect if file exists, otherwise pass through

On a website that I maintain, there used to be a redirect in place that would redirect a request to the corresponding .php file, like so: www.example.com/press-room -> www.example.com/press-room.php. However, if the php file did not exist, it would pass the request through to WordPress, which would then look for a page with the slug 'press-room'.

Well, in the past couple of days, the .htaccess file was reset, and the redirect is no longer in place. How can I re-implement this rule without interfering with WordPress? That is, how can I implement this redirect only if the php file exists?

Upvotes: 2

Views: 1458

Answers (1)

Jon Lin
Jon Lin

Reputation: 143886

Try this above your wordpress rules:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

First, make sure it's not a directory. Second, check if appending the .php to the end gives you an existing file. Finally, do the rewrite.

Upvotes: 2

Related Questions