Total Pageviews

Thursday, 16 January 2014

从链接中去掉.html的办法

修改apache的配置文件如下:
 <VirtualHost *:80>
ServerName yourdomain.com
DocumentRoot /path/to/your-site-root-dir
<Directory /path/to/your-site-root-dir>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
 allow from all
 </Directory>

</VirtualHost>

需加上上述蓝色部分,重启apache即可。

(参见http://briteming.blogspot.co.uk/2014/01/linux-vpsnodejs-harp.html)
下面从第4段开始的代码并不总是有效!因此还是建议修改apache的配置文件更好。
--------------------------------------

Ok so, after some research, and failing to achieve this with a rewrite rule, the following line of code worked:

redirectMatch 301 ^(.*)\.html $1

This is quite usefull to remove any url extension and avoid broken links, hopefully helps someone in the future...
 -------------------

Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.

Options +FollowSymLinks -MultiViews
DirectorySlash Off

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

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

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]

---------------------------------------

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


用如下代码则更好:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*\.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ %{REQUEST_FILENAME}.html


有人用的是如下代码:
RewriteCond %{REQUEST_FILENAME}/ -d
RewriteCond %{REQUEST_FILENAME}.html !-f
RewriteRule [^/] - [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)\.html$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
------------------------------------------------------------------------

Try this to putting in your .htaccess file:
Redirect permanent www.mysite.de/cool-shoes.html www.mysite.de/cool-shoes

this may be helpful to you.

from http://stackoverflow.com/questions/10245032/url-rewrite-remove-html/10279744#10279744