简体   繁体   English

.htaccess mod_rewrite规则不起作用

[英].htaccess mod_rewrite rule not working

I'm trying to get mod-rewrite to work in a htaccess file. 我正在尝试使mod-rewrite在htaccess文件中工作。

The URL I'm trying to achieve is: 我试图获得的URL是:

blog/{id}-{title}/

So for example: 因此,例如:

http://localhost/blog.php?id=12&title=this-is-a-test

Would become: 会成为:

http://localhost/blog/12-this-is-a-test/

No what I've been trying is: 没有我一直在尝试的是:

RewriteEngine On
RewriteRule ^blog/([0-9]+)-([a-zA-Z-])/$ blog.php?id=$1&title=$2

When I go to http://localhost/blog/12-this-is-a-test/ I get a 404 error but if I go to http://localhost/blog.php the test page with the test text displays as expected. 当我转到http://localhost/blog/12-this-is-a-test/出现404错误,但如果转到http://localhost/blog.php则带有测试文本的测试页显示为预期。

Maybe I've written the rule incorrectly, if so can anyone suggest edits or are there any good tutorials, I've been reading through the http://httpd.apache.org/docs/2.0/misc/rewriteguide.html but haven't been able to figure out where I'm going wrong. 也许我写的规则不正确,如果有人可以建议修改或有任何好的教程,我已经阅读了http://httpd.apache.org/docs/2.0/misc/rewriteguide.html,但是没有无法找出我要去哪里。

Any help is greatly appreciated or even pointing me in the right direction, thanks. 非常感谢您的帮助,甚至为我指明了正确的方向,谢谢。

EDIT: 编辑:

I was getting this on every link that the redirect rule applied: 我在重定向规则应用的每个链接上都得到了这个:

http://localhost/C:/Apache/htdocs/test5/blog/1-Lorem-ipsum-dolor-sit-amet

After I added in the below to the vhost: 在将以下内容添加到虚拟主机后:

<Directory "{path}">
    Options All               
    AllowOverride All
    Order allow,deny
    Allow from all
</Directory>

And I had to clear out the browser cache as it was also causing issues. 而且我还必须清除浏览器缓存,因为它也会引起问题。 Once these two actions where taken, anubhava's answer below worked and solved my issues. 一旦采取了这两项行动,下面的anubhava答案就可以解决我的问题。

Your regex is missing quantifier + which is causing 2nd capture group to fail since it is expecting only 1 character: 您的正则表达式缺少量词+ ,这将导致第二个捕获组失败,因为它仅预期1个字符:

Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^blog/([0-9]+)-([a-zA-Z-]+)/?$ blog.php?id=$1&title=$2 [L,NC,QSA]

Also better to turn off MultiViews since your php file name blog.php is same as directory name blog . 另外,最好关闭MultiViews因为您的php文件名blog.php与目录名blog相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM