Htaccess For Mod Rewrite Of URLs

#1

Sitepoint Members,

I've been working on removing the .html extension in the address bar, in other words change /test.html to /test, using htaccess code, but I can't find the code to do it.

I wonder if I have to have an /index.html file, but I tried the methods below on index.html and couldn't get /index.html to /index . In the case of index.html I don't want "index" either, instead from /index.html to just /.

Assuming at least one of the methods below is correct, what might I be missing that would make it not work in my test folder. I have a folder labeled test and inside I have index.html (with simple, working webpage code) and test.html (with the same code as index.html) and an htaccess file.

I've found many methods but none work. In case it matters, I tried the code below with an without rewritebase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
[How to remove .php, .html, .htm extensions with .htaccess](http://www.sicanstudios.com/how-to-remove-php-html-htm-extensions-with-htaccess/)
--
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html [L,QSA]
[How to remove file extensions from URLs | dave dash](http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/)
--
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.html?q=$1 [L,QSA]
[How to remove file extensions from URLs | dave dash](http://davedash.com/2006/07/26/how-to-remove-file-extensions-from-urls/)


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Remove File Extension Using htaccess | Web, Mobile Development


RewriteRule ([a-zA-Z0-9]+)/?$ /$1.html [L]
[Removing file extensions with .htaccess [Archive] - bluehostforum.com](http://www.bluehostforum.com/archive/index.php/t-9964.html)


RewriteCond /%{REQUEST_FILENAME}.php -f
RewriteRule ^([a-zA-Z0-9_-\s]+)/$ /$1.php
Remove File Extention from URLs


RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^.]+)\.php$ $1 [L]
[remove html and/or PHP extensions in URL - CSS-Tricks Forums](http://css-tricks.com/forums/discussion/3814/remove-html-and-or-php-extensions-in-url/p1)


RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
How to remove file extensions using .htaccess | Free Script Installation Service


RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
[Plain Text Passwords: Remove HTML and PHP File Extensions using .htaccess](http://blog.spruce.it/2011/01/remove-file-extensions-html-and-php.html)

Thanks,

Chis

#2

The nicest option is

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

It's simply the most elegant and fool proof. I don't know why it won't work for you though.
May try adding Options +FollowSymLinks at the top?

#3

ScallioXTX,
Thanks for writing.

I can't figure out why it won't work. In my htaccess I have

Use PHP5 Single php.ini as default

AddHandler application/x-httpd-php5s .php .htm .html
RewriteEngine on
RewriteBase /
Options +SymLinksIfOwnerMatch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
FileETag None
Header unset ETag
<Files .htaccess>
deny from all
</Files>

One part was changed automatically to SymLinksIfOwnerMatch, it's the only one my webhost allows.

When I go to mysite.com/samplepage I get 404 error
when I go to mysite.com/samplepage.html the page comes up

Thanks,

Chris

#4

If you do this:

RewriteRule ^(.*)$ http://www.google.com/?$1.html [L,R=302]

where does it take you?

#5

ScallioXTX,
I get http://www.google.com/?samplepage.html.html

and

http://www.google.com/?index.html.html

It seems to be adding an .html instead of removing the one that exists.

Use PHP5 Single php.ini as default

AddHandler application/x-httpd-php5s .php .htm .html
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ http://www.google.com/?$1.html [L,R=302]
Options +SymLinksIfOwnerMatch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
FileETag None
Header unset ETag
<Files .htaccess>
deny from all
</Files>

Chris

#6

If it does that I'd say that you requested "samplepage.html" instead of just "samplepage". Did you? If so, wasn't it the whole point to rid of the .html?

#7

ScallioXTX,
I see what you're saying, you're correct that I wasn't going to /index and /samplepage.

Unfortunately, and without the google code, when I request
/index I get a 404 error and no change to the address in the address field.
When I request
/index.html the page comes up and no change to the address

Same for /samplepage and /samplepage.html.

I used ctrl F5 throughout to make sure caching wasn't getting in the way.

Here's the htaccess

Use PHP5 Single php.ini as default

AddHandler application/x-httpd-php5s .php .htm .html
RewriteEngine on
RewriteBase /

RewriteRule ^(.*)$ http://www.google.com/?$1.html [L,R=302]

Options +SymLinksIfOwnerMatch
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html
FileETag None
Header unset ETag
<Files .htaccess>
deny from all
</Files>

Thanks,

Chris

#8

wait a minute

#9

I corrected that one line to
RewriteCond %{REQUEST_FILENAME}\.html -f

but it didn't fix the problem.

I made sure I used ctrl f5

#10

ScallioXTX,
I got it.
The RewriteBase / line was causing the problem.

I would have found the problem when I was testing for RewriteBase but I had myself distracted when I was testing /samplepage.html instead of /samplepage

Somehow I had it in my head that going to /samplepage.html would change automatically to /samplepage, like a redirect.

Someone pointed out to me that sometimes an htaccess file at a higher level can cause problems with a lower level htaccess and that having both these lines in this sequence
RewriteEngine Off
RewriteEngine On

can help check for htaccess conflicts.

This wasn't the case but it's nice to know.

Thanks,

Chris

#11

Glad you got it working smile

#12

ScallioXTX,
I noticed both /page and /page.html. That's going to be duplicate content bad for seo/google isn't it?

Thanks,

Chris

#13

ScallioXTX,
I found this code on another Sitepoint page and from the context I thought it would eliminate the problem and result in /page/ or /page but page.html wouldn't exist.

RewriteCond %{REQUEST_URI} !\.(js|jpe?g|gif|css|swf|html|ico|png|xml|xsl|rdf|txt|gz|php)$ [NC]
RewriteRule ^([^/]+)$ index.php?pageName=$1 [L]

I could change index.php to index.html but I really have no idea what to put in for "pagename". Maybe just a forward slash or nothing ?
That seems to easy to be correct. Even if it is correct will it apply to all .html pages or just index?

Thanks,

Chris

#14

That rule's got nothing to do with what you want, ditch it smile

Take a look at the section "Redirect TO New Format" here Apache's mod_rewrite

#15

ScallioXTX,
In their example,
"http://www.example.com/display.php?country=USA&state=California&city=San_Diego

could be changed to

http://www.example.com/USA/California/San_Diego"

if the "change" is made, when one goes to the original

"http://www.example.com/display.php?country=USA&state=California&city=San_Diego"

what will happen? It's still active since it's now a go between.

I think I'm going to not remove the .html, there's no way to remove .html without causing seo problems. I thought htaccess could be used to acknowledge /page and without the browsers and search engines knowing the existence of /page.html. It doesn't look like this is possible.

Thanks,

Chris

#16

That's the first section of the article, I told you to have a look at the section "Redirect TO New Format" which is somewhere down the page.

How do you figure?

This certainly is possible; the code in the second post of this thread does just that.

#17

ScallioXTX,
Both /page and /page.html come up fine, when only /page should come up.

I've tested /page.html several times making sure I used ctrl F5, /page.html comes up fine when I should be getting a 404 error.

Thanks,

Chris

#18

ScallioXTX,
I think the solution is, if your site is not about html, don't use htaccess and instead use .htm (25% less information (no L) than html. Simce Google uses only the first 65 or so characters of an adresss, just make sure your address is 65 or more characters before the extension.

I think

/1-234-567-........65.htm
is much more seo advantageous than
/1word-2word

meaning that while having your adress end with no / and no extension seems to be the most advantageos for seo, it's not true if the address with no / and no extension is short when compared to an address that exceeds in length google's reach to the extension.

What do you think?

Chris

#19

I think I'd like to see some sources where you got that info. TBH what you're saying makes no sense to me whatsoever (and is actually more appropriate for a discussion in the SEO forum than here. Maybe you should start a thread there?).

#20

ScallioXTX,
Sorry about that. I'll take it over to the SEO forum.

Chris