.htaccess基本网址重写

时间:2013-05-31 21:04:10

标签: apache .htaccess mod-rewrite

我想对网站的网址做一些事情,我似乎无法查看.htaccess文件。

1删除文件扩展名f.e. example.com/file.php应为example.com/file

2删除www。 F.E. www.example.com应为example.com(我已将此部分用于工作,但如果在我修复之后我又讨厌这个部分

3没有人应该能够在根f.e的末尾看到index.php。 example.com/index.php应为example.com

4我的博客页面应该有很好的网址f.e. example.com/blog.php?article=the-name-of-article应为example.com/blog/the-name-of-article

这是我当前的.htaccess文件

重写网址

Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /

## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php

## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]

## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^blog/(.*)$ blog.php?article=$1 [QSA,L]

当我尝试转到blog/the-name-of-article时,我收到内部服务器错误。

2 个答案:

答案 0 :(得分:0)

删除.php:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

请参阅Remove .php extensions with .htaccess without breaking DirectoryIndex

修改 对于漂亮的网址,请查看本教程:http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

答案 1 :(得分:0)

从您的.htaccess的正文和评论中看来我过去会提供它:P

你的.htaccess中唯一错误的是规则的排序。始终将它们从最具体到最通用。你的代码是这样的:

Options +FollowSymLinks -MultiViews
rewriteengine on
RewriteBase /

## remove www
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
## remove ugly part of url for blog.php
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^blog/(.*)$ /blog.php?article=$1 [QSA,L]

## Hide .php extension
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php