首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >.htaccess url重写格式不正确

.htaccess url重写格式不正确
EN

Stack Overflow用户
提问于 2013-07-20 12:00:29
回答 2查看 372关注 0票数 1

目前,我在MAMP中通过我的本地主机工作。我已经阅读和研究了适当的方法来允许.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:

代码语言:javascript
代码运行次数:0
运行
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-20 14:37:06

将现有的.htaccess代码完全替换为以下代码:

代码语言:javascript
代码运行次数:0
运行
复制
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]
票数 2
EN

Stack Overflow用户

发布于 2013-07-20 13:15:29

替换

代码语言:javascript
代码运行次数:0
运行
复制
RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1

通过以下方式:

代码语言:javascript
代码运行次数:0
运行
复制
RewriteRule ^(about|contact|this|that|search)/\?$ index.php?p=$1

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17758509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档