简体   繁体   中英

Strange URL RewriteRule .htaccess Issue needs explaination

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?

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, ie 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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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