目前,我在MAMP中通过我的本地主机工作。我已经阅读和研究了适当的方法来允许.htaccess文件url重写,并取得了成功。但是现在,该文件根本没有按照预期对链接进行格式化。例如,我有三个页面:index.php
、about.php
和contact
。我使用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>
发布于 2013-07-20 06:37:06
将现有的.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]
发布于 2013-07-20 05:15:29
替换
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1
通过以下方式:
RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1
我刚刚逃脱了?使用斜杠,因为,否则,你不这样做,问号将被解释,并将意味着它之前的斜杠是可选的。
https://stackoverflow.com/questions/17758509
复制