2016-02-10 02:04

.htaccess重写; 但需要变量


Hello Stackers,

I Currently have a Problem with my HTACCESS file. It needs to rewrite a Url, which it does, but I still need a Variable to pass through.

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

The Link used is however, after the rewrite, no variables are recognizable, but I need the value of the REF variable.

Is there A way to do this? Also, I would preffer to still get if(isset($_GET['ref'])){ working.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答


  • duanchi1230 duanchi1230 5年前

    This is my standard .htaccess file which I use for all of my rewrite websites...

    ## 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]

    Using this file everything in the URL if it is not a valid directory or file is ignored and routed to index.php in the site root. I then access it as a single paramater using $_SERVER['REQUEST_URI'] which I can explode using explode("/", $_SERVER['REQUEST_URI']), this gives me an array starting at index 0 of each value. Using this script I am still able to access $_GET variables as long as at the end of the URI the convention ?var_name=value has been used.

    This has been tested and works with Apache, PHP5 and works both debian based linux operating systems as well as Windows installs of Apache.

    点赞 评论 复制链接分享