• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Engaged ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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!

Views

2.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 18, 2019 Mar 18, 2019

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=

...

Votes

Translate

Translate
LEGEND ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

osgood_  wrote

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.

Well, I only tried yours (just now) but only the .php part since -- so far -- all pages are php anyway.

And not only does it work, but it works both ways... did you even know that when you put it together? With and without the .php extension works.

It's not exactly what I was looking for in the OP, but upon further revision, what I was asking for in the OP would've opened entirely new cans of worms to solve; and I really need to wrap this thing up soon. So thanks! This is perfect.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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

osgood_   wrote

Well, I only tried yours (just now) but only the .php part since -- so far -- all pages are php anyway.

And not only does it work, but it works both ways... did you even know that when you put it together? With and without the .php extension works.

I did not try it with the .php/.html extension still included in the links but if it works with those still present then that's great.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

osgood_ If I understand how the following code works...

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !^/pages/

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

...then what the server is doing is "shadow-adding" the characters "/pages" between the TLD and the first chr in the "href",

Correct?

Then what would be the most elegant way to incorporate exceptions?

In other words, how do I link to /img/image.jpg when the server keeps shadow-adding "/pages" before it (therefore looking for /pages/img/image.jpg) resulting in a 404?

Is there something I can insert somewhere that will tell the server not to touch certain 'exception' links (and take those target destinations more literally)?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jun 24, 2020 Jun 24, 2020

Copy link to clipboard

Copied

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

When this rule is applied-

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

2)This part will work on server

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

3)But if we test on devserver it will throw page not found bcoz .html is missing.

So to avoid this i have written script that will append .html

4)I WANTED TO ASK that if we removed .html from href and configured htaccess will our pages be accessible to google bot bcoz i heard that google  bot crawls the links and than follows that link to crawl rest of the pages?

 

Likes

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 18, 2019 Mar 18, 2019

Copy link to clipboard

Copied

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 & Moderator
Alt-Web Design & Publishing ~ Web : Print : Graphics : Media

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 25, 2020 Jun 25, 2020

Copy link to clipboard

Copied

LATEST
#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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines