0

我正在为我的组织设置网址缩短服务。我想将缩短的 url重写为我的 index.php 文件,但将请求重定向到我的主网站的根目录,即:

请求:short.domain.tld/abc
用户看到:short.domain.tld/abc
提供的页面:short.domain.tld/index.php?code=abc

请求:short.domain.tld/
用户看到:website.domain.tld
提供的页面:website.domain.tld(在我的网站服务器上)

我的 .htaccess 文件目前是:

Options +FollowSymLinks 
RewriteEngine on

RewriteCond %{REQUEST_FILENAME}  !-s 
RewriteRule ^/(.*)$ index.php?code=$1 [L] 
RewriteRule ^.*$ http://website.domain.tld/

我不确定如何区分对子目录的请求和对域根的请求。

4

1 回答 1

1

您可以在根目录的 .htaccess 文件中尝试此操作:

Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST}  short\.domain\.tld  [NC]
RewriteCond %{REQUEST_URI}  !index\.php       [NC]
RewriteRule  ^([^/]+)/?   /index.php?code=$1  [NC,L]

RewriteCond %{HTTP_HOST}  short\.domain\.tld  [NC]
RewriteCond %{REQUEST_URI}   ^/$      
RewriteRule  .*    http://website.domain.tld  [R=301,L]

内部映射

http://short.domain.tld/abc带或不带斜杠(显示在地址栏中)

http://short.domain.tld/index.php?code=abc

假设字符串abc是动态的。


或永久重定向

http://short.domain.tld/没有段路径

到:

http://website.domain.tld/ (显示在地址栏中)

于 2013-03-07T03:36:53.193 回答