Skip to main content
Inspiring
March 18, 2019
Answered

Using .htaccess to clean up hyperlinks and urls...

  • March 18, 2019
  • 3 replies
  • 3223 views

Hi,

A few years ago (before I knew enough to start coding on my own) a friend of mine set up one of my websites so that I never had to use file extensions in my hyperlinks.

What the server would do in the absence of an extension is cycle through a list of favored ones (specified by me) until it found a match. In other words, if I'm linking to just "/contact" (no extension) then the server would search the target folder for "contact.php" (assuming .php is first on my list of extensions to search for) and failing that, it'll search for the next extension (.html); and so on...

The purpose is merely elegance, nothing more; I find that linking to "/contact" looks better than linking to "/pages/contact.html" (and also gives less away about the BTS structure of the site).

I'm already using the following code (provided by one of you superheroes a couple of months back) to remove /pages from the url.:

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !^/pages/

RewriteRule ^(.*)$ /pages/$1

If someone could tell me how to edit this so that I don't even have to specify extensions (because the server will know what extensions to search for, in order) then the UX would be so clean you could eat off it (all links would be simply /contact, /about, /portfolio, etc.)

I'm sure I could achieve the same results by creating a subfolder for each page, putting an index file in each subfolder and linking to said subfolder rather than a file... but talk about unnecessary bloat, if the same can be achieved with a simple addition to the .htaccess file.

Can it?

Thanks!

This topic has been closed for replies.
Correct answer osgood_

You can address both php and html pages like below:

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.html -f

RewriteRule ^(.*)$ $1.html

You will have to go through your website links though and change them to the format below (removing the .php or .html) extension:

AFTER

<a href="pages/about">About</a>

<a href="pages/contact">Contact</a>

BEFORE

<a href="pages/about.php">About</a>

<a href="pages/contact.html">Contact</a>

3 replies

Community Expert
June 26, 2020
#REMOVE PHP EXTENSIONS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]

 

I usually add this too, I suspect Google is smart enough to know /mypage and /mypage.php are the same:

 

#301 REDIRECT ANY .PHP PAGE REQUESTS TO EQUIVELANT PAGE WITHOUT EXTENSION
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L]

 

You can always use canonical tag to prevent any issues.

Paul-M - Community Expert
Nancy OShea
Community Expert
March 18, 2019

By default browsers look for an index page inside the folder.  

This URL  yourdomain . com / contact /

will take users to

yourdomain . com  / contact / index.foo  where foo is any file extension.

Nancy O'Shea— Product User & Community Expert
Brainiac
March 18, 2019
Under S.Author
Inspiring
March 18, 2019

osgood_  wrote

Have a look at the link below:

https://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/

Thanks, but that doesn't really set up a list of extensions to search for in the absence of one (ie, what I'm trying to achieve). It only works with one extension.

Still, it DOES clean up at least the most-used extension (.php) so I'm not going to look a gift horse in the mouth and will try to get this working for now.

Is there a way to incorporate it with the code I had already quoted in the OP, or do I just add it after, like below?

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !^/pages/

RewriteRule ^(.*)$ /pages/$1

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

I guess what I'm asking is... do you see any redundancy there that could be tightened up, or is this the correct way to code it?

Thanks! Between you, Ben and Nancy always being so helpful, it's almost like I can code.

Brainiac
March 18, 2019

What's the difference between the code at the link you sent me to earlier...

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

...and this code you're giving me now?

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php

The syntaxes seem wildly different to my admittedly-ignorant eyes.


https://forums.adobe.com/people/Under+S.  wrote

What's the difference between the code at the link you sent me to earlier...

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

...and this code you're giving me now?

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^(.*)$ $1.php

The syntaxes seem wildly different to my admittedly-ignorant eyes.

I think you will find there are several different ways that one can approach solving the issue of removing page extensions. I read through the original link I posted and it became obvious that the poster had not found a way to also include removing the .html AND .php extension names so some more Google reseach suggests it can be done using the second approach posted, which does work as I have just tested it out.