Strange URL RewriteRule .htaccess Issue needs explaination

为君一笑 提交于 2019-12-13 01:27:17

问题


I have URLs like book.php?id=23 and using htaccess to make them book/id/title I tried this

RewriteRule ^book/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]

but it didn't work, it was forwarding to books.php but no $_REQUEST data it turned out it was because the url reference was the same as the name as the php file I was calling with help from the community to work around this worked

RewriteRule ^books/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]

this is my current working .htaccess:

RewriteEngine On
RewriteRule ^kitap/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
RewriteRule ^yyin/([0-9]+)/([^/]+)/?$ yayin.php?id=$1 [NC,L,QSA]
RewriteRule ^proje/([0-9]+)/([^/]+)/?$ project.php?id=$1 [NC,L,QSA]

PeteR:

every online tutorial I've ever seen uses examples like ^product and product.php, or ^category and category.php so I don't know why this should be. Maybe someone else can shed some light on this?


回答1:


I started messing with this after our small discussion on your original question and I'm starting to think it depends on the server configuration and whether or not it will run extensionless scripts.

My dev server, for example, will run extensionless scripts, i.e. typing /xyz will display xyz.php

A rewrite rule of

RewriteRule ^category/([0-9]+)/([^/]+)/?$ category.php?id=$1 [NC,L,QSA]

does not work, however

RewriteRule ^categories/([0-9]+)/([^/]+)/?$ category.php?id=$1 [NC,L,QSA]

does work.

On my hosting server, which does not support extentionless scripts, www.mydomain.com/xyz will give me a 404 error, but:

RewriteRule ^category/([0-9]+)/([^/]+)/?$ category.php?id=$1 [NC,L,QSA]

works as expected.

I can only assume that when the server supports extensionless scripts then the rewrite rule is ignored if the alias is identical to the script name.




回答2:


Most likely you have MultiViews on which conflicts with rewrite rules.

Fulle .htaccess: (Goes in DOCUMENT_ROOT directory)

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^kitap/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]
RewriteRule ^yyin/([0-9]+)/([^/]+)/?$ yayin.php?id=$1 [NC,L,QSA]
RewriteRule ^proje/([0-9]+)/([^/]+)/?$ project.php?id=$1 [NC,L,QSA]

RewriteRule ^books/([0-9]+)/([^/]+)/?$ book.php?id=$1 [NC,L,QSA]


来源:https://stackoverflow.com/questions/20656432/strange-url-rewriterule-htaccess-issue-needs-explaination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!