.htaccess url重写不正确格式化

时间:2013-07-20 04:00:29

标签: php .htaccess mod-rewrite

目前我在MAMP中通过我的localhost工作。我已经阅读并研究了允许.htaccess文件url重写并且成功的正确方法。但是现在,文件根本没有格式化链接。例如,我有三个页面index.phpabout.phpcontact。我正在使用MVC来处理我网站的框架。这是.htaccess文件或本地服务器中的代码有问题吗?

当从一个页面转到另一个页面时,链接看起来像这样:

localhost/mvc/index.php?

localhost/mvc/index.php?p=about

localhost/mvc/index.php?p=contact

.htaccess应将链接格式化为:

localhost/mvc/index/?

localhost/mvc/about/?

localhost/mvc/contact/?

htaccess的:

<IfModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Set the base to this directory:
RewriteBase /mvc/

# Redirect certain paths to index.php:
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1

</IfModule>

2 个答案:

答案 0 :(得分:2)

使用以下代码完全替换现有的.htaccess代码:

Options +FollowSymLinks -MultiViews

DirectoryIndex index.php index.html

# Turn mod_rewrite on
RewriteEngine On
# Set the base to this directory:
RewriteBase /mvc/

# Redirect /mvc/index.php?p=page to /mvc/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\?p=([^\s]+) [NC]
RewriteRule ^ %1/? [R=302,L]

# Redirect /mvc/index.php to /mvc/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+mvc/index\.php\s [NC]
RewriteRule ^ /mvc/ [R=302,L]

# Internally forward certain /mvc/page/ to /mvc/index.php?p=page
RewriteRule ^(about|contact|this|that|search)/?$ /mvc/index.php?p=$1 [L,QSA,NC]

答案 1 :(得分:0)

替换

RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1

with:

RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1
我刚逃过了?使用斜杠,因为否则,你不这样做,问号将被解释,并且意味着它之前的斜线是optinal。