我有数百个dynamic创build的url – foo.com/123,foo.com/contact等
我想redirect所有这些url(不知道所有这些,只知道有az或1-9)只是foo.com。
请让我知道如何使用Apache重写规则可以做到这一点
我曾尝试: RewriteRule ^(.*)$ index.php
但是这不起作用。
正如Brian所回答的那样:
RewriteCond %{REQUEST_URI} !index.php RewriteRule ^(.*)$ /index.php [R=301]
正如上面Chris所build议的那样:
RewriteCond %{REQUEST_URI} !index.php RewriteRule ^(.*)$ /index.php [R=307]
如果您正在尝试执行内部redirect,那么URL不会在浏览器中重写:
RewriteCond %{REQUEST_URI} !index.php RewriteRule ^(.*)$ /index.php
布赖恩的方法将做301redirect。 这将告诉任何search引擎/浏览器/caching内容已经永久移动,现有的url不见了,不应该使用。 克里斯的方法将做一个307redirect。 307是临时redirect的HTTP / 1.1等价物。 两者都会在浏览器中重写url,两者都会影响到该网站的用户,点击该url并被redirect到foo.com/index.php的新url。 最后一个方法将在内部重写url,以便浏览器中的url保持为foo.com/contact而不是foo.com/index.php
没有任何规则实际上将任何parameter passing给index.php,所以301和307redirect实际上会丢失url的其余部分。 如果您使用301/307redirect将所有内容redirect到门页,则此行为可能是可以接受的。 如果这是你的意图,我会build议使用:
ErrorDocument 404 /index.php
然后确保所有的图片/链接都在该页面中相对/绝对链接,或者使用a来确保在结果页面上正确地重写url。
如果你想传递请求的URI到index.php作为parsing参数:
RewriteCond %{REQUEST_URI} !index.php RewriteRule ^(.*)$ /index.php?page=$1
在这种情况下,$ _REQUEST ['page']将包含来自foo.com/contact的/ contact
您的重写规则将导致无限循环,因为index.php也匹配^(.*)$
。 你应该能够做到这一点(未经testing):
RewriteCond %{REQUEST_URI} !index.php RewriteRule ^(.*)$ /index.php [R=301]
额外的[R=301]
将返回一个“永久移动”的redirect状态码。
基于他的url结构,他可能不想要[R = 301],因为他试图制作漂亮的url。