dongnianchou7047 2016-02-10 02:04 采纳率: 0%
浏览 42
已采纳

.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 example.com/ref/value 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.

  • 写回答

1条回答 默认 最新

  • duanchi1230 2016-02-10 06:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题