1

I am using an .htaccess file to hide the .php extensions in URLs. Although it is working fine, I now have to also hide the .php file extension in all the links that are shown on the page.

For example, when I hover the mouse over the link, its URL is showing the status bar and includes the .php extension. How can I remove the .php from the link URLs too or completely hide the URL in the status bar of the browser?

I'm using the following code in my .htaccess file:

Options +FollowSymLinks -MultiViews # Turn mod_rewrite on RewriteEngine On RewriteBase / ## don't touch /forum URIs RewriteRule ^forums/ - [L,NC] ## hide .php extension snippet # To externally redirect /dir/foo.php to /dir/foo RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R,L] # To internally forward /dir/foo to /dir/foo.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*?)/?$ $1.php [L] # To remove www header RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^(.*)$ http://%1/$1 [L,R=301] 

(Code originally based on this answer by anubhava.)

The links in my HTML file look like menu1.php, menu2.php, etc. When I click on these links the URL shown in the browser URL bar looks like domain.com/menu1, which is good. But in the status bar, when I hover over these links, it still shows menu1.php.

How can I hide that .php suffix in the status bar? Do I have to manually change all the URLs in all the links, or there is some way I can hide them?

3
  • Not sure about this, but just remove the .php in <a href=""> Commented Aug 24, 2017 at 15:00
  • Show the code or whatever where you output the hyperlinks. Commented Aug 24, 2017 at 15:00
  • yes, lets see code Commented Aug 24, 2017 at 15:05

1 Answer 1

4

Short answer: Yes, you do need to edit all your links to remove the .php extension from them.


The .htaccess file you've quoted contains two (relevant) parts that do different things:

# To externally redirect /dir/foo.php to /dir/foo RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC] RewriteRule ^ %1 [R,L] 

This RewriteRule triggers when the user's browser requests an URL with a .php extension. The rule causes the webserver to respond with an external redirect to the same URL, but with the .php suffix removed. The browser will then retry loading the page with the new URL, and will also show that new URL in the address bar.

# To internally forward /dir/foo to /dir/foo.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*?)/?$ $1.php [L] 

This second RewriteRule then triggers on URLs that don't include the .php extension, but which would correspond to actual .php files on your webserver if the extension was appended to them. The rule then appends then .php extension back onto the URL and retries the request — but it does so internally, inside your webserver, without the browser ever knowing about the change.

Thus, in effect, the browser is told to show the URL without the .php extension, but the webserver still ends up running the PHP script with the extension in place.

However, all this only happens after the user has clicked the link. The URL rewrite rules in the .htaccess file do not, and cannot, magically edit your HTML code to fix the URLs included in them. Therefore, to actually hide the fact that you're using PHP, you will need to manually edit your HTML (including any HTML code output by your PHP scripts themselves) to remove the .php filename extensions from the URLs.

(As a side effect, doing so also makes the first RewriteRule quoted above unnecessary. However, you'll probably want to keep it anyway, so that any old links that still include the .php extension in the URL will still get automatically redirected.)

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

1 Comment

This should definitely have a lot more upvotes

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.