1

目前我在 MAMP 中通过我的本地主机工作。我已经阅读并研究了允许重写 .htaccess 文件 url 并成功的正确方法。但是现在,该文件根本没有按照需要格式化链接。例如我有三个页面index.phpabout.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/?

.ht 访问:

<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>
4

2 回答 2

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]
于 2013-07-20T06:37:05.930 回答
0

代替

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

和 :

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

我刚刚逃脱了?用斜线,否则,你不这样做,问号将被解释,并意味着它之前的斜线是可选的。

于 2013-07-20T05:15:29.427 回答