简体   繁体   English

.htaccess重写网址和查询

[英].htaccess rewrite url and query

I couldn't find anything with the search so I'll try it here. 我无法在搜索中找到任何内容,因此我将在这里尝试。

I need to make a rewriting rule (for a CMS) and additionally to avoid any bot or unwanted person to come onto the site i also want to set a custom token in the database which can be varied from site to site. 我需要制定一个重写规则(针对CMS),另外还要避免任何漫游器或有害人员进入该站点,我还想在数据库中设置一个自定义令牌,该令牌可能因站点而异。

So for example a URL would look like this: 因此,例如,URL如下所示:

http://example.org/pageGoesHere/?token=kabaza

Now I need to get the page ("pageGoesHere") and the token separately, but the token must not be set all the time, as when the site is online, the token won't be needed anymore to access the page. 现在,我需要分别获取页面(“ pageGoesHere”)和令牌,但是不必一直设置令牌,因为当网站处于联机状态时,就不再需要令牌来访问页面。

What I have tried so far: 到目前为止我尝试过的是:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{QUERY_STRING} token=(.*)$
RewriteRule (.*) ./index.php?url=$1&token=$2

Whatever I try, it never returns me both the url and the token (if specified) as $_GET variable. 无论我尝试什么,它都不会同时以$ _GET变量返回URL和令牌(如果已指定)。

Hopefully anyone can help me to solve this. 希望任何人都可以帮助我解决这个问题。

Your rewrite rule contains two backreferences ($1 and $2) but the pattern only has one set of parentheses. 您的重写规则包含两个后向引用($ 1和$ 2),但是该模式只有一组括号。 To match backreferences from the most recent rewrite condition, use %1 要匹配来自最新重写条件的反向引用,请使用%1

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{QUERY_STRING} token=(.*)$
RewriteRule (.*) ./index.php?url=$1&token=%1 [L]

RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) ./index.php?url=$1

Edit: Just looking through the documentation and I suspect this will work, though I've never used the QSA flag before: 编辑:只浏览文档 ,我怀疑这会起作用,尽管我之前从未使用过QSA标志

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) ./index.php?url=$1 [QSA]

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

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