Static HTML: remove www, no file extensions, force slash

New to mod_rewrite? This is a good place to start.

Static HTML: remove www, no file extensions, force slash

Postby jog » Mon Sep 21, 2009 12:34 pm

Hi

Thanks to this forum I could make it work... almost. What I can't seem to get right is to force the slash at the end like in http://domain.tld/page/

My page is http://jonathangubler.ch - here's what I have in my .htaccess:

Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

# Remove /index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.html$ /%1 [R=301,L]

# Remove www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L]

# Remove .html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

# Add .html back.
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule ^(.*[^/])$ /$1.html [QSA,L]
jog
 
Posts: 2
Joined: Mon Sep 21, 2009 2:40 am

Postby richardk » Tue Sep 22, 2009 9:25 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Remove /index.html
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.html(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.html$ http://example.com/%1 [R=301,L]

# Remove www.
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,L]

# Remove .html
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.html$ /$1/ [R=301,L]

# Add .html back.
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.*[^/])/$ /$1.html [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jog » Tue Sep 22, 2009 1:32 pm

Thanks for your suggestion! I think I tried that before - the rewrite itself works fine, but the links to the images and css are broken, and entering the address without a trailing slash results in a 404 error...
jog
 
Posts: 2
Joined: Mon Sep 21, 2009 2:40 am

Postby marcelloma » Tue Sep 22, 2009 1:38 pm

jog wrote:Thanks for your suggestion! I think I tried that before - the rewrite itself works fine, but the links to the images and css are broken, and entering the address without a trailing slash results in a 404 error...


check out the faqs:
viewtopic.php?p=10709#10709

I personally use the full path solution, always adding the www.website.com part before any element, better use PHP with a method echo Info::getSiteUrl(); so you can easily change all the links at once if you happen to change url.
marcelloma
 
Posts: 9
Joined: Sat Sep 12, 2009 4:14 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 34 guests

cron