.htaccess - 短网址系统: How to redirect the Custom URLs?

标签 .htaccess mod-rewrite redirect

我正在尝试为我的公司提供类似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/

中使用此 .htaccess
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/

相关文章:

php - 请求实体太大 apache

redirect - SSL 域重定向到不相关的 URL?

php - 重写个人资料页面的 URL

php - Prestashop 在数据库中更改旧域后重定向到旧域

php - 通过子目录中的 htaccess 从 URL 中删除目录名称

PHP:如何检查用户是否已经登录,否则重定向到登录页面

php - htaccess 或 php 重定向到此任务?

php - 将 URL 重写为动态页面

debugging - 除了使用浏览器之外,是否有更好的方法来测试或调试 Mod 重写规则?

php - 我希望我的用户只访问我的 php 文件,如果他们尝试访问我想包含我的页面 404 文件的文件夹