如何重写给定的 URL

如何重写给定的 URL

我怎样才能重写

www.mysite.com/someURLhere

进入

www.mysite.com/ping.php?url=someURLhere

不要误将本地文件和目录视为域。

所以我不想

www.mysite.com/index.php 
www.mysite.com/admin/

重写为

www.mysite.com/ping.php?url=index.php
www.mysite.com/ping.php?url=admin/

答案1

尝试一下这个破解的 joomla 的 htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/ping.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule ^(.*)$ ping.php?url=$1 [L]

答案2

我可能更容易将 URL 重定向到实际上不存在的“虚拟”子文件夹中。我不确定这是否适合您的设计,但我绝对建议您考虑它,因为它可以让您在编写重写规则时更轻松,还可以防止您意外重写存在的文件。

www.mysite.com/p/someURLhere

(其中 /p/ 实际上并不存在于文件系统中)重定向到 www.mysite.com/ping.php?url=someURLhere

那么你的重写规则就需要更少的条件。

答案3

DennyHalim 的评价

我对其进行了进一步的修改(没有尝试,但看起来是对的:s)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/ping.php
RewriteCond %{REQUEST_URI} (/.*)$  [NC]
RewriteRule ^(.*)$ ping.php?url=$1 [L]

基本上说 '如果 url 没有指向存在的文件','如果 url 没有指向存在的目录','如果 url 不是“/ping.php”','匹配所有内容','重写为“ping.php?url=$1”'

相关内容