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.

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

报告相同问题?

悬赏问题

  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥20 win11无法启动 持续蓝屏且系统还原失败,无法开启系统保护
  • ¥15 哪个tomcat中startup一直一闪而过 找不出问题
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码