douduanque5850 2015-12-21 19:15
浏览 20
已采纳

除非我把index.php,我没有收到$ _GET ['url']

I have a vhost called quick-mvc and I have a .htaccess file in the root dir. I have mod_rewrite.so activated. I know that mod_rewrite is not recommended but it is what I am using now and would like to get this working. Thank you!

.htaccess file

RewriteEngine on

RewriteCond %[REQUEST_FILENAME] !-d
RewriteCond %[REQUEST_FILENAME] !-f
RewriteCond %[REQUEST_FILENAME] !-l

RewriteRule ^(.+)$ index.php?url=$1 [L]

in the browser ex 1

http://quick-mvc/one/two/three/test

$_GET['url'] returns "index.php"

in the browser ex 2

http://quick-mvc/index.php/one/two/three/test

$_GET['url'] returns "index.php/one/two/three/test"

What I would like to accompish I would like to return what ex 2 shows even whether I add index.php like ex 1. I would also like index.php not to show up. Though I can code this out easily with php.

  • 写回答

1条回答 默认 最新

  • duanjue6575 2015-12-21 19:35
    关注

    Your example reminds me of how Drupal used to approach url rewriting to their front controller. Checking Drupal 7, they have changed the way they do this.

    Drupal 7 .htaccess (stripped down):

      RewriteEngine On
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_URI} !=/favicon.ico
      RewriteRule ^ index.php [L]
    

    You could take a similar approach, and then in index.php:

    <?php 
    $url = $_SERVER['REQUEST_URI'];
    $parts = parse_url($url);
    

    The php function parse_url should give you the path and query string.

    $your_url = $parts['path'];
    

    If placed in the web root, and we visit this url:

    http://example.com/foo/bar/baz?bat=qux
    

    parse_url gives:

    array
      'path' => string '/foo/bar/baz'
      'query' => string 'bat=qux'
    

    Peek at some of the other frameworks to see how they do it.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度