【问题标题】:http.conf redirect example.com/foo/bar to example.com/foo?id=barhttp.conf 将 example.com/foo/bar 重定向到 example.com/foo?id=bar
【发布时间】:2011-11-14 13:15:51
【问题描述】:

我正在尝试创建一个动态页面,其内容由传递给它的变量确定,但我希望 url 很好。基本上我想在目录 /foo 中有一个处理任何条的单个索引文件。 example.com/foo/bar 到 example.com/foo?id=bar

我已经尝试了我在 httpd.conf 文件中能想到的一切。我最近的尝试是:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foo/([a-z-]*) /foo/index.php?id=$1 [QSA,L]

但是,当我尝试加载 example.com/foo/bar 时,加载的页面是 example.com

非常感谢任何帮助!

编辑:当我访问 example.com/foo/bar 时,它仍在加载 example.com/index.php 也许我的其他规则之一正在干扰?

AllowOverride none
Options +FollowSymLinks
RewriteEngine on

RewriteRule ^(.*)\.html$ http://www.example.com/$1.php [R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/foo/([a-z-]*)(\/?)$ /foo/index.php?id=$1 [NC,QSA,L]

RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]

.html 到 .php,我想要实现的目标,以及 example.com 到 www.example.com。

【问题讨论】:

    标签: .htaccess mod-rewrite redirect httpd.conf


    【解决方案1】:

    我在我的 httpd.conf 中使用它

    <VirtualHost *:80>
        DocumentRoot "/var/www/"
        ServerName www.domain.com   
        ServerAlias www.domain.com
        <Directory /var/www/>
            #other directives
        </Directory>    
        <Directory /var/www/foo/>   
            RewriteEngine on
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d 
            RewriteRule ^([a-z]+)(\/?)$ index.php?id=$1 [NC,QSA,L]
        </Directory>
    </VirtualHost>
    

    【讨论】:

    • 你能给我看看你的 httpd.conf 吗?也许你有另一个覆盖这个的规则
    • 我编辑了我的答案。不要在 ^/foo/([a-z-]*)(\/?)$ 中的“^”后添加“/”
    • 您可能需要将 AllowOverride none 更改为 AllowOverride All
    • 还是没有运气。 example.com/foo/bar 只是加载 example.com - 我正在编辑 httpd.conf 文件,而不是 .htaccess 所以 AllowOverride 应该无关紧要。 - 无论如何都试过了,也没有运气。
    • 哦,那就用目录索引作为 foo 路径。看我的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-09
    • 1970-01-01
    • 2022-11-04
    • 2014-03-20
    相关资源
    最近更新 更多