Page 1 of 1

.htaccess to hide file extensions

Posted: Sat Jul 07, 2007 11:54 am
by superdez
How can I hide .jpg, .gif, .html and .php in my urls ? The files in my server have these extensions but I don't want them to show in the actual urls. I'm using the following but does not work. Have any other better ways of achieving the same goal ? I don't want any redirection error codes such as 301 etc... I don't want to hide .txt and .xml. I'm also currently parsing .html as php.




RewriteEngine on

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

Posted: Sat Jul 07, 2007 1:02 pm
by superdezign
That's because your RewriteRule doesn't make sense. In fact, your RewriteCond doesn't make sense either.

In reality, all that you'll need is a RewriteRule that takes the whole URI and gets the extension. If it doesn't match, you don't change it. Otherwise, just keep everything but the extension.

Code: Select all

RewriteRule (.*)\.(ext1|ext2)$ $1

Posted: Sat Jul 07, 2007 6:40 pm
by superdez
Thank you for your input. Is there any way to restrict this rule to: NOT apply to my indexed pages from google and yahoo since I am already applying the following rules to the indexed pages from google and yahoo?


RewriteRule ^Category_Folder/Category_File.html$ /category-folder/category-file [R=301,L,NC]

Posted: Sat Jul 07, 2007 7:53 pm
by superdezign
Have you tried it, yet? The rules are ran in the order that they are written.

Posted: Sun Jul 08, 2007 7:15 am
by superdez
Ok, I think more information is needed:

Currently google and I use the following url-method and this file-method uploaded on the server: http://www.domainname.com/Category_Fold ... _File.html

for SEO reasons I would like to change to: http://www.domainname.com/category-folder/category-file

I have managed to change all my files to lowercase and the hyphen but I cannot erase the file extension because DreamWeaver cannot work with extensionless files.

So before I upload my new file-method, I have to setup rewriterules for google's current index and for my new file-method:

I think that the first rule should be customized for google and the second should be used for rewriting without extension. What do you suggest ? What do I need to change to make this run smoothly ? I think that this first rule won't work because it will point to a non-existing file (error 404) right ?

RewriteRule ^Category_Folder/Category_File.html$ /category-folder/category-file [R=301,L,NC]

RewriteRule (.*)\.(ext1|ext2)$ $1

Posted: Sun Jul 08, 2007 7:44 am
by superdezign
That doesn't seem to make sense, and I don't believe that hyphens are more SEO-friendly than underscores. In fact, I'm pretty damn sure that Google sees no difference. They are both valid characters.

But your first RewriteRule doesn't make any sense, considering that it will do absolutely nothing except for one URI, 'Category_Folder/CategoryFile.html,' which I am pretty sure doesn't exist.

Posted: Sun Jul 08, 2007 8:11 am
by superdez
I'm just implementing the w3c's recommendations: http://www.w3.org/TR/chips/#uri

You're right, that url does not exist. Its just a generic url that is typical in my site. My future .htaccess would have 1 rewriterule for each page that I need to change. I know that it is not a very efficient way of doing things but it is the only way I know, that is why I am seeking help !

Posted: Sun Jul 08, 2007 9:03 am
by superdezign
This isn't tested, but I think that as long as you don't have the L flag, this would replace your underscores with hyphens:

Code: Select all

((.*?)_) $2-
As far as I know, RewriteRule continues to work on the URL until it either fulfills no more rules, or hits the [L] flag. Also, you may be interested in URL parsing through PHP to avoid so much mod_rewrite.

Posted: Sun Jul 08, 2007 1:44 pm
by superdez
I uploaded my site and tested all types of solutions including:

RewriteRule (.*)\.(ext1|ext2)$ $1

but it "redirects" to an extensionless file that obviously does not exist and therefore a 404 error shows up.

Actually a modification of my first post works:

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

I've tried:

options +multiviews

and it also actually works but unfortunately it sends 2-3 extra headers stating where the real files are located (the ones with extensions), which is not what I want for SEO reasons.

Posted: Sun Jul 08, 2007 6:08 pm
by superdezign
I'm really starting to think you don't understand that 'ext1' and 'ext2' are meant to be replaced with file extensions. :?

And what are these "SEO reasons" that you keep referring to? Google doesn't search through your headers to find where a file originally existed. It doesn't matter to the search engine or the users of the search engine.

Posted: Mon Jul 09, 2007 8:11 am
by superdez
Come on superdezign, of course I realized that but it really doesn't matter since now it works. :oops:


This site describes some problems they had with google and multiviews:

http://www.gerd-riesselmann.net/archive ... multiviews

(Note that these people also decided to get rid of the file extension for this page)

Posted: Mon Jul 09, 2007 8:27 am
by superdezign
superdez wrote:http://www.gerd-riesselmann.net/archive ... multiviews

(Note that these people also decided to get rid of the file extension for this page)
No, those people never had a file extension for that page. The page exists purely through templating and databasing.

Posted: Mon Jul 09, 2007 8:36 am
by superdezign
Wait a second... You mean *that's* what you've been trying to do all this time? My responses were under the assumption that you'd already been serving your pages through mod_rewrite, and just liked having an extension at the end of the link. Now, all of that code you've been posting makes sense.

I've been giving you the complete opposite answer. :P
You've gotta be more clear. x_x