.htaccess重写;但需要变量

时间:2016-02-10 02:04:39

标签: php .htaccess mod-rewrite

Hello Stackers,

我目前在HTACCESS文件中遇到问题。它需要重写一个Url,但它仍然需要一个Variable来传递。

RewriteRule ^ref/(.*)$ /quickregister/start.php?ref=$1

使用的链接是example.com/ref/value但是,在重写之后,没有变量可识别,但我需要REF变量的值。

有没有办法做到这一点?此外,我还是会让if(isset($_GET['ref'])){继续工作。

1 个答案:

答案 0 :(得分:0)

这是我用于所有重写网站的标准.htaccess文件......

## No directory listings
IndexIgnore *

## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes

## Mod_rewrite in use.

RewriteEngine On

# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.

## Begin - Custom redirects
#
# If you need to redirect some pages, or set a canonical non-www to
# www redirect (or vice versa), place that code here. Ensure those
# redirects use the correct RewriteRule syntax and the [R=301,L] flags.
#
## End - Custom redirects


RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

如果URL不是有效的目录或文件,则使用此文件中的所有内容将被忽略并路由到站点根目录中的index.php。然后我使用$_SERVER['REQUEST_URI']作为单个参数访问它,我可以使用explode("/", $_SERVER['REQUEST_URI'])进行爆炸,这给了我一个从每个值的索引0开始的数组。使用此脚本,我仍然可以访问$ _GET变量,只要在URI的末尾使用约定?var_name=value

这已经过测试,可以与Apache,PHP5一起使用,可用于基于debian的linux操作系统以及Apache的Windows安装。