【问题标题】:mod_rewrite is not working with my urlsmod_rewrite 不适用于我的网址
【发布时间】:2013-12-08 04:09:22
【问题描述】:

我正在尝试使使用 apache mod_rewrite 的 SEO 友好 URL。

我的正常网址是这样的 -

index.php?p=about
index.php?p=contact
index.php?p=this

我期望的 SEO 友好 URL 应该与此类似 -

localhost/php_advance/ch02/about
localhost/php_advance/ch02/contact
localhost/php_advance/ch02/this

我尝试创建一个.htaccess 文件并对我的apache httpd.conf 文件进行一些更改。

这是我的 .htaccess 文件

<ifModule mod_rewrite.c>

# Turn on the engine:
RewriteEngine on

# Set the base to this directory:
RewriteBase /php_advance/ch02/

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

</ifModule>

另外,在httpd.conf 文件的末尾,我添加了一些这样的代码 -

<Directory "C:/wamp/www/php_advance/ch02">
 AllowOverride All
</Directory>

NOTE:我正在使用 WAMP 服务器和 Windows 07

但是这种编码对我不起作用。希望有人能帮助我。 谢谢你。

【问题讨论】:

  • @anubhava,不,我不能得到 404。当我在浏览器地址栏上输入 http://localhost/php_advance/ch02/about 时,我可以得到这个 Not Found The requested URL /php_advance/ch02/about was not found on this server.
  • 试试这条规则来测试:RewriteRule ^(about|contact|this|that|search)/?$ index.php?p=$1 [L,R]
  • @anubhava 我检查了,但仍然无法正常工作。
  • 似乎 .htaccess 未启用。在上面放一些垃圾文本,看看会不会产生500(内部服务器)错误
  • 需要在哪个页面添加垃圾文字? .htaccess 或 ???

标签: php apache .htaccess mod-rewrite friendly-url


【解决方案1】:

试试这个:

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteBase /php_advance/ch02/
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /php_advance/ch02/index.php [L]
</IfModule>

【讨论】:

  • 当你说它不起作用时,怎么办?当您在http://localhost/php_advance/ch02/about 对您的服务器执行curl -I 时,标头响应是什么?
  • 然后我收到了这条消息 - Not Found The requested URL /php_advance/ch02/about/ was not found on this server.
  • 普通网址是这样的 - http://localhost/php_advance/ch02/index.php?p=about
  • @Win8_learner,我浏览了你给的帖子,但我无法从中得到解决方案。
【解决方案2】:

确保 mod_rewrite 已打开。在您的 httpd.conf 文件中,您应该有类似的内容:

LoadModule rewrite_module modules/mod_rewrite.so

然后确保您的 htaccess 文件在您的 /ch02/ 目录中。

【讨论】:

  • Jon Lin,我需要将这行代码LoadModule rewrite_module modules/mod_rewrite.so 插入到我的httpd.conf 文件中吗?是的,我的 .htaccess 文件在我的 /ch02/ 目录中。
  • 我检查了我的 httpd.conf 文件,发现 mod_rewrite 已打开。这是文件中的那一行 - LoadModule rewrite_module modules/mod_rewrite.so
猜你喜欢
  • 2018-10-14
  • 2011-02-09
  • 2018-10-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 2013-11-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多