【问题标题】:htaccess wildcard redirect to query stringhtaccess 通配符重定向到查询字符串
【发布时间】:2012-07-27 13:17:17
【问题描述】:

简单的问题,我已经看到它被问了数百次,但我似乎无法让它发挥作用(我感到羞耻)。

我正在尝试将 index.php 以外的任何内容重定向到 view.php?id=$1 ,其中 $1 是 index.php 以外的任何内容 - 但我似乎无法让它工作。

例如:

http://domain.com/ 应该使用 index.php
http://domain.com/index.php 应该这样
但是..
http://domain.com/sdgoi3 应该使用http://domain.com/view.php?id=sdgoi3

我已经尝试了一些事情,并通过上述问题进行了调查,但无济于事。

有人有解决办法吗?赞赏。

【问题讨论】:

    标签: .htaccess


    【解决方案1】:

    尝试将其放入文档根目录的 htaccess 文件中:

    RewriteEngine On
    RewriteRule ^$ /index.php [L]
    RewriteRule ^index\.php - [L]
    RewriteRule ^view\.php - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /view.php?id=$1 [L]
    

    这里的逻辑是:

    1. 如果请求URI是/,则重写为index.php
    2. 如果请求URI以index.php开头,请不要更改并通过
    3. 如果请求URI以view.php开头,请不要更改并通过
    4. 如果请求是针对不存在的文件或目录,则使用 id 参数传递给view.php

    【讨论】:

    • 谢谢,这适用于通配符,但不适用于http://domain.com/ 等。另外,有没有办法让 /css/* /img/* 正常工作?
    【解决方案2】:

    也许是这样的:

    RewriteCond %{REQUEST_URI} !^index\.php
    RewriteCond %{REQUEST_URI} !^view\.php
    RewriteRule ^(.*)$ view.php?id=$1 [L]
    

    ?

    【讨论】:

    • 已编辑以修复 view.php 上的循环。但乔恩·林的答案可能是好的。
    猜你喜欢
    • 2014-05-10
    • 2015-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-11-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-02
    相关资源
    最近更新 更多