首页 文章

apache .htaccess redirect - 干净网址

提问于
浏览
1

我正在开发一个php重定向脚本,当他们访问重定向网址时302会将访问者重定向到其他网站 .

该脚本从URL获取变量(id),然后将访问者重定向到特定页面 .

网址结构为: example.com/redirect/index.php?id=test

目前,如果我使用“丑陋的”网址,所有重定向都会起作用,但我想通过.htaccess重写从网址中删除所有不必要的信息,以获得更好的可用性 .

我需要哪些.htaccess重写规则才能使上面显示的网址看起来像: example.com/redirect/test

我目前正在使用以下.htaccess规则

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>

但它们只适用于像 example.com/redirect/index.php?id=test 这样的网址,如果我尝试 example.com/redirect/test 我得到一个404错误页面 .

可能很高兴知道,我有2个.htaccess文件,一个在我的根目录中,一个在root / redirects /目录中 .

最好的祝福 !

1 回答

  • 0

    试试这个:

    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/?   [NC]
    

相关问题