【问题标题】:How to set url rewriting in .htaccess file?如何在 .htaccess 文件中设置 url 重写?
【发布时间】:2017-10-17 19:21:28
【问题描述】:

我有一个使用此 url 重定向的重定向脚本

http://my-domain.com/URL.php?url=http://www.google.com

我想把网址改写成

http://my-domain.com/?url=http://www.google.com

http://my-domain.com/?http://www.google.com

我知道这可以通过 .htaccess 来完成,所以我自己尝试了一些代码。我在我的 web 目录中创建了一个 .htaccess 文件,并输入了一些代码,例如:

# Prevent Apache from serving .ht* files:
<FilesMatch "^\.ht">
Order allow,deny
Deny from all
</FilesMatch>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.my-domain.com$ [NC]
RewriteRule ^(.*)$ http://my-domain.com/$1 [R=301,L]
RewriteRule ^embed/([^\.]+)\.php$ /$1 [L]
RewriteRule URL.php /

但它仍然无法正常工作,我在这里做错了什么

【问题讨论】:

  • 我不认为这需要用php标记

标签: .htaccess mod-rewrite


【解决方案1】:

试试这个。

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^url/(.*)$ /URL.php?url=$1 [L]

示例:http://yoursite.com/url/http://www.google.com

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^go/(.*)$ /URL.php?url=$1 [L]

示例:http://yoursite.com/go/http://www.google.com

这样使用会更好。

【讨论】:

  • 我忘了添加 Get 参数。我希望它有效。现在你可以试试了。
  • 并添加了 RewriteBase。
  • 当我使用 yoursite.co/url/?url=http://www.google.com 时它可以工作,但脚本不起作用我的意思是它没有重定向,当我删除 htaccess 代码时它工作正常...
  • 我告诉过你我忘记添加参数了。你试过我最近编辑的代码吗? /URL.php?url=$1
  • 很高兴你做到了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-04-23
  • 1970-01-01
  • 2015-12-03
  • 1970-01-01
  • 1970-01-01
  • 2013-03-29
  • 2011-04-08
相关资源
最近更新 更多