我正在尝试为我的公司提供类似tinyurl的服务,到目前为止看起来不错,但现在我遇到了一个无法解决的问题。
假设我生成的 URL 是“www.thecompanyiworkfor.com/shorturl/2jh62/”。我的猜测是我必须使用一些脚本,比如说“redirect.php”,我可以在其中访问数据库,查找该短网址,找到原始网址,然后使用 header 进行重定向。
我的问题是,如何使“www.thecompanyiworkfor.com/shorturl/2jh62/”打开“redirect.php”并且可以访问“shorturl”作为参数?我想我必须对 .htaccess 做一些事情,但我不太确定我应该做什么...
请帮忙!
最佳答案
这是我的建议。
1) 创建子域 (s.thecompanyiworkfor.com)。由于该文件夹与主 WWW 文件夹分开,因此管理起来会更容易,并且可以避免与 .htaccess 发生冲突。
例如:
s.thecompanyiworkfor.com => /home/thecompanyiworkfor.com/s_public_html/
www.thecompanyiworkfor.com => /home/thecompanyiworkfor.com/public_html/
2) 在 /home/thecompanyiworkfor.com/s_public_html/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !^index\.php
RewriteRule ^([a-z0-9\-]+)(\/?)$ index.php?code=$1 [L,NC,QSA]
然后在您的 /home/thecompanyiworkfor.com/s_public_html/index.php
中,您可以检查哪个代码对应于哪个 URL 并重定向。如果未找到,请重定向至 www.thecompanyiworkfor.com
关于.htaccess - 短网址系统: How to redirect the Custom URLs?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7498410/