mod_rewrite的的.htaccess有20%转化为 - 转化为、mod_rewrite、htaccess

2023-09-02 11:27:42 作者:逝言已陌生

我一直在念叨了几个小时的.htaccess文件,现在,我想我开始的想法,但我还是需要一些帮助。我发现各种 answers around左右,但仍不能确定如何做到这一点。

I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.

据我理解你写的,你要'prettify每一页扩展一个规则,所以如果你有 something.php anotherpage.php thispage.php 等,他们都希望(将收到??)的参数,每个需要自己的规则。这是正确的?

As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?

我要改变这个网站的网址有像这样的,

The site I want to change has urls like this,

maindomain.com/sue.php?r=word1%20word2

和至少一个页面有两个参数

and at least one page with two arguments

maindomain.com/kevin.php?r=place%20name&c=person%20name

因此​​,我想提出的是

So what I would like to make is

maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/

保持这个PHP页面,使它看起来像目录。大部分时间我已经阅读处理如何删除到的参数传递的PHP页面的教程。不过,我想保留它。

Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.

我forseeing的问题是,所有的URL的的.php?R = 部分是相同的,即 sue.php·R = kevin.php?R = 和的.htaccess决定哪些URL会发生变化的文件名,然后忽略它。如果我想保留的文件名会我必须要改变的?R = 所以,这是个人?我希望这是有意义的。到目前为止,我有这个,但我敢肯定是行不通的。

the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r= so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.

Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1

我想我一定要添加([^ ​​- ] *)这在某些部分或某些方式,以便它检测到的%20 部分的URL,但我怎么把它转换为 - 。此外,如何是我的 $ _ GET 函数去上班?

And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??

我希望我的问题是有道理的。

I hope my question makes sense

推荐答案

您错过空间某处这些规则,但我认为你已经在做2个独立的规则正确的想法。更难的问题是将所有的 - 为空格。让我们开始转化为GET变量:

You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:

# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]

RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]

这些将采取一个URI,看起来像 /苏/胡说/

提取部分 检查 /document_root/sue.php 确实存在 重写 /苏/胡说/ /sue.php?r=blah Extract the sue part Check that /document_root/sue.php actually exists rewrite /sue/blah/ to /sue.php?r=blah

同样适用于2个字的URI

Same thing applies to 2 word URI's

类似于 /凯文/富/酒吧/

提取凯文部分 检查 /document_root/kevin.php 确实存在 3重写 /凯文/富/酒吧/ /kevin.php?r=foo&c=bar Extract the kevin part Check that /document_root/kevin.php actually exists 3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar

现在,摆脱了 - ,并将其更改为空格:

Now, to get rid of the "-" and change them to spaces:

RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3 %4 [L]

这看起来有点凌乱,但条件的查询字符串匹配,寻找一个 C = R = 在查询字符串,匹配针对 - C = r的值= ,然后重写查询字符串替换 - (注意:空间得到EN codeD作为 20%)。这将删除所有的 - 在GET参数的值实例 C 研究,并用空格代替他们。

This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.