简体   繁体   English

.htaccess多个重写规则/优先级

[英].htaccess Multiple Rewrite Rules / Prioritizing

Here is my current htaccess file: 这是我当前的htaccess文件:

RewriteEngine On

RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/gotourl.php?urlid=?%1 [L]
RewriteRule    ^(\w+)$ ./gotourl.php?urlid=$1    [NC,L]    
RewriteRule    ^(\w+)/$ ./gotourl.php?urlid=$1    [NC,L]   

The website in question is a URL shortener I'm working on for testing/developing and personal use. 有问题的网站是我正在测试,开发和个人使用的URL缩短器。 The above code may not be ideal as I'm not that familiar with .htaccess and made this via googling and testing. 上面的代码可能不是理想的,因为我不太熟悉.htaccess并通过谷歌搜索和测试来实现。 This does what I want it to for the URL shortening aspect: 对于URL缩短方面,这就是我想要的:

http://website.com/1234 -> http://website.com/gotourl.php?urlid=1234
http://website.com/1234/ -> http://website.com/gotourl.php?urlid=1234

The problem is I cannot figure out how to get it to ignore certain directories. 问题是我无法弄清楚如何使其忽略某些目录。 I want specific directories to go to specific pages: 我希望特定目录转到特定页面:

http://website.com/img (or) http://website.com/img/ -> http://website.com/img.php
http://website.com/img/1234 (or) http://website.com/img/1234/ -> http://website.com/viewimage.php?imgid=1234

The main problem is I don't know how to properly do this and the ways I've tried to do it would always rewrite to: 主要问题是我不知道如何正确执行此操作,而我尝试执行的方法将始终重写为:

http://website.com/gotourl.php?urlid=XXXX

or end up in an infinite loop of redirects instead of going to the specific page I'm wanting. 或最终导致重定向的无限循环,而不是转到我想要的特定页面。 Any help is appreciated. 任何帮助表示赞赏。

In essence I'm looking for help writing my htaccess to do the following: 本质上,我正在寻找编写htaccess的帮助以执行以下操作:

URL user visits -> URL user sees
http://website.com/img -> http://website.com/img.php
http://website.com/img/XXXX -> http://website.com/viewimage.php?imgid=XXXX
http://website.com/XXXX -> http://website.com/gotourl.php?urlid=XXXX

All paths should work with or without the trailing slash. 所有路径都可以使用斜杠,也可以不使用斜杠。

Looking for help to get it to do that with maybe a bit of explanation as to what each line actually means so I can learn from it. 寻求帮助以使其做到这一点,可能需要对每行的实际含义进行一些解释,以便我可以从中学习。

The main thing you want to change, is making your rule more specific. 您要更改的主要内容是使规则更加具体。 Your shortened url can probably not contain a slash or a dot, so you could change both the url shortening url's to: RewriteRule ^([^/\\.]+)/?$ gotourl.php?urlid=$1 [NC,L] . 缩短的网址可能不能包含斜线或点,因此您可以将两个缩短的网址都更改为: RewriteRule ^([^/\\.]+)/?$ gotourl.php?urlid=$1 [NC,L] You'll need to prevent 'gotourl.php' from matching that though. 不过,您需要阻止“ gotourl.php”与之匹配。 The rule for img alone is more specific and should be done first. 仅适用于img的规则更为具体,应首先完成。 The rule for img/something is a little less specific and can be anywhere. img /某物的规则不太具体,可以在任何地方。

RewriteEngine On

RewriteRule ^img/?$ img.php [L]
RewriteRule ^img/([^/]+)/?$ viewimage.php?imgid=$1 [L]
RewriteCond %{HTTP_HOST} !^www\.website\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.website\.com [NC]
RewriteRule ^(.*)$ http://www.website.com/gotourl.php?urlid=%1 [L]
RewriteRule ^([^/\.]+)/?$ gotourl.php?urlid=$1 [NC,L]

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

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