Axion
Axion

Reputation: 722

Removing .php Extension from Specific URL

I've tried searching but can't find a solution that works.

I'd like to remove the .php extension and add a trailing slash from a specific URL (about.php).

For instance...

www.example.com/about.php should redirect to www.example.com/about/

and

www.example.com/about should redirect to www.example.com/about/

I've tried various RewriteRules in .htaccess but none of them worked the way I needed them to.

The following adds the trailing slash, and I can access the page without the extention... but the .php version still shows up.

RewriteEngine On
RewriteBase /
RewriteRule ^about/$ about.php [END,QSA,NC]

Does anyone have any idea how to get this working?

Upvotes: 1

Views: 244

Answers (2)

MrWhite
MrWhite

Reputation: 45829

To externally redirect /about.php (and /about - no trailing slash) to /about/ (for the benefit of search engines and external third parties that may have indexed/linked to the old URL) you would need to add something like the following before your existing rewrite:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about(\.php)?$ /about/ [R=302,L]

The condition that checks against the REDIRECT_STATUS environment variable is to ensure we only redirect direct requests and not already rewritten requests by the later rewrite. REDIRECT_STATUS is empty on the initial request and gets set to the HTTP status (ie. "200" on success) after the first rewrite.

The RewriteRule pattern matches both /about and /about.php and redirects to /about/.

Note that this is a 302 (temporary) redirect. Only change it to a 301 (permanent) - if that is the intention - once you have confirmed it works OK. This is to avoid caching issues (301s are cached persistently by the browser). Likewise, if you have previously experimented with 301s then you will need to make sure the browser is cleared before testing.

UPDATE#1: If I wanted to do this to multiple pages

If you have just two pages (as in your example/comment) then you can combine both rules into one (rather than duplicating the rule block). For example:

RewriteEngine On
RewriteBase /

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(about|contact)(\.php)?$ /$1/ [R=302,L]

RewriteRule ^(about|contact)/$ $1.php [END,QSA,NC]

The $1 backreference will hold either "about" or "contact" from the capturing group in the RewriteRule pattern.

UPDATE#2: ... I'm starting to add a lot of pages and I'm afraid the list will get out of control. Is there a better way to handle tons of pages... like 50+?

If all your pages are files in the document root then you could generalise the regex and essentially rewrite everything. For example:

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([\w-]+)(\.php)?$ /$1/ [R=302,L]

RewriteRule ^([\w-]+)/$ $1.php [END,QSA,NC]

This is no longer "specific", as it redirects/rewrites any URL that simply "looks like" a valid request.

\w is the shorthand character class for any word character (ie. a-z, A-Z, 0-9 and _), to this we add the hyphen. So your URLs/filenames can only consist of those characters.

The above will match /about/, /contact/, /something-else/ and /foo_BAR_123/, etc. But it won't match /foo.jpg, /foo/about/ or /foo.bar/, etc.

If required, you could first check that the file exists before rewriting to it (or redirecting), but that is relatively expensive and probably not required here.

Upvotes: 1

rudy_dark_1
rudy_dark_1

Reputation: 54

You can use index.php instead. index page does not show up on browser address.

You can create http://www.example.com/about/index.php and on the browser, it will show as:

http://www.example.com/about/

If you need to redirect from http://www.example.com/about.php I can recommend use header http-equiv:

< meta http-equiv="refresh" content="0; URL='http://www.example.com/about/'" />

Upvotes: 1

Related Questions