.htaccess to hide file extensions
Moderator: General Moderators
.htaccess to hide file extensions
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)$
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.(php|html|jpg|gif)$ -f
RewriteRule ^(.*)$ $1.(php|html|jpg|gif)$
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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)$ $1Thank 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]
RewriteRule ^Category_Folder/Category_File.html$ /category-folder/category-file [R=301,L,NC]
Last edited by superdez on Sun Jul 08, 2007 6:42 am, edited 1 time in total.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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
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
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.
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 !
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 !
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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:
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.
Code: Select all
((.*?)_) $2-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.
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.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
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.
Come on superdezign, of course I realized that but it really doesn't matter since now it works.
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)
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)
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
No, those people never had a file extension for that page. The page exists purely through templating and databasing.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)
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
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.
You've gotta be more clear. x_x
I've been giving you the complete opposite answer.
You've gotta be more clear. x_x