【问题标题】:RewriteRule on localhost本地主机上的重写规则
【发布时间】:2017-11-24 20:36:42
【问题描述】:

本地主机,xampp,赢 7
站点根文件夹是s02,所以所有相关文件,包括.htaccess都在这个文件夹下。

这样的链接可以正常工作:
http://localhost/news/s02/view.php?art=160915142500&title=blue-sky

我想要以下计算的 url
http://localhost/articles/160915142500/blue-sky

.htaccess

RewriteEngine on
RewriteBase /
RewriteRule ^articles/([0-9]+)/([a-zA-Z-_]*)$ view.php?art=$1&title=$2 [NC,L]

结果 - 错误 404

httpd.conf
LoadModule rewrite_module modules/mod_rewrite.so - 未注释
AllowOverride All

.htaccess (test) - 这行得通:

RewriteEngine on
RewriteRule ^ http://example.com/? [L,R]  

phpinfo();
mod_rewriteLoaded Modules

有什么帮助吗?

【问题讨论】:

    标签: php .htaccess mod-rewrite


    【解决方案1】:

    试试下面的规则,我们告诉 apache 传入的 uri 不是文件或目录。

    RewriteEngine on
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^articles/([0-9]+)/([a-zA-Z-_]*)$ /news/s02/view.php?art=$1&title=$2 [NC,L]
    

    【讨论】:

    • 这只有在我将.htaccess 放在C:\xampp\htdocs 文件夹中并且view.php 已加载但没有依赖项(图像、css、js ......)时才有效。有没有办法将htaccess 保存在C:\xampp\htdocs\news\s02 文件夹中并获取所有元素的view.php
    • 使用<base href>标签正确加载css和js。
    • base href 工作,谢谢。根文件夹呢?如果我将htaccess 保留在htdocs 中,它将适用于我所有的本地站点。我需要每个站点都有自己的htaccess,如果有的话。
    • 这样做将 htaccess 放入新闻目录并尝试我编辑的规则。
    • 你的意思是news/s02。站点根文件夹是s02。所有文件,包括 index.php、view.php... 都在那里。
    【解决方案2】:

    .htaccess 文件放入根文件夹,即 xampp 的 htdocs(我猜)。 .htaccess 文件应该是这样的。

    RewriteEngine on
    RewriteBase /
    RewriteRule ^articles/([0-9]+)/([a-zA-Z-_]*)$ news/s02/view.php?art=$1&title=$2 [NC,L]
    

    【讨论】:

    • 在我的系统上运行良好。创建场景<root_folder>/news/s02/view.php 并将与上面相同的.htaccess 文件放入root_folder。
    • 尝试重启你的 xampp 服务器
    • 你的根文件夹是什么,请问。这是我的完整路径:C:\xampp\htdocs\news\s02.htaccess 在 `C:\xampp\htdocs` 下
    • 我正在使用 wamp 服务器,所以我的根文件夹是 C:\wamp\wwwview.php 文件的完整路径是 C:\wamp\www\news\s02\view.php
    • 除非必要,否则请确保您的 `C:\xampp\htdocs\news\s02` 文件夹中没有任何 .htaccess 文件。
    猜你喜欢
    • 1970-01-01
    • 2018-05-06
    • 2014-01-23
    • 2014-07-23
    • 1970-01-01
    • 2014-11-30
    • 2016-01-07
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多