.htaccess网站重定向

时间:2012-11-08 21:24:55

标签: .htaccess

我需要对我的网站进行更改,但我不想将它们发送到我的真实帐户,直到我确定它们是正确的。我的帐户是GoDaddy,根据他们的说法,我唯一能做的就是将我的所有文件移到另一个文件夹并通过转到该文件夹​​来测试我的更改。我不使用网络浏览器调用Duphil.com,而是使用Duphil.com/DuphilWork。 .htaccess文件中的编码不允许我留在此文件夹中。

以下是编码:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

ErrorDocument 404 /index.php?page=404
ErrorDocument 403 /index.php?page=403

RewriteRule ^$ /index.php
RewriteRule ^about/$ /index.php?page=about
RewriteRule ^oilfield/$ /index.php?page=oilfield
RewriteRule ^pipeline/$ /index.php?page=pipeline
RewriteRule ^safety/$ /index.php?page=safety
RewriteRule ^sales/$ /index.php?page=sales
RewriteRule ^contact/$ /index.php?page=contact
RewriteRule ^stateLicensures/$ /index.php?page=stateLicensures
RewriteRule ^keyPersonnel/$ /index.php?page=keyPersonnel
RewriteRule ^projectListings/$ /index.php?page=projectListings
RewriteRule ^legal/$ /index.php?page=legal
RewriteRule ^techDivision/$ /index.php?page=techDivision

RewriteRule ^equipment/$ /index.php?page=equipment
RewriteRule ^projectImages/$ /index.php?page=projectImages
RewriteRule ^contact/(.*)/$ /index.php?page=contact&contactName=$1

我该如何做到这一点?

1 个答案:

答案 0 :(得分:0)

.htaccess的做法非常混乱。每个页面的规则都有许多缺点,其中最重要的是,如果您添加页面,则必须更新.htaccess。另一个缺点是您不能简单地添加例外,因为您必须为每个规则添加。但是,我们可以做的一件事就是将页面重写为自己,并告诉它一旦完成就不应该继续重写:

RewriteRule ^DuphilWork/(.*)$ DuphilWork/$1

RewriteRule ^$ /index.php
[..]

现在,这意味着您仍然必须在测试网站上编写您的网址而不进行重写。换句话说,他们必须是这样的:Duphil.com/DuphilWork/index.php?page=oilfield

如果您想在测试网站中编写更好的网址,那么当前设置的唯一方法是复制.htaccess中的所有规则并将DuphilWork添加到匹配项中模式和重定向路径在其中一个副本。

真正的解决方案当然是有这样一条规则:

RewriteCondition !-f
RewriteRule ^(.*)$ /index.php?page=$1

然后确保index.php正确解析该页面字符串(现在包括确保该页面存在。在这种情况下,解决当前问题变得微不足道。但是,我相信你应该能够这与我上面给出的解决方案有关。


使用我上面描述的涉及复制所有内容的策略,你最终会得到类似的东西:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

ErrorDocument 404 /index.php?page=404
ErrorDocument 403 /index.php?page=403

RewriteRule ^DuphilWork/$ /DuphilWork/index.php
RewriteRule ^DuphilWork/about/$ /DuphilWork/index.php?page=about
RewriteRule ^DuphilWork/oilfield/$ /DuphilWork/index.php?page=oilfield
RewriteRule ^DuphilWork/pipeline/$ /DuphilWork/index.php?page=pipeline
RewriteRule ^DuphilWork/safety/$ /DuphilWork/index.php?page=safety
RewriteRule ^DuphilWork/sales/$ /DuphilWork/index.php?page=sales
RewriteRule ^DuphilWork/contact/$ /DuphilWork/index.php?page=contact
RewriteRule ^DuphilWork/stateLicensures/$ /DuphilWork/index.php?page=stateLicensures
RewriteRule ^DuphilWork/keyPersonnel/$ /DuphilWork/index.php?page=keyPersonnel
RewriteRule ^DuphilWork/projectListings/$ /DuphilWork/index.php?page=projectListings
RewriteRule ^DuphilWork/legal/$ /DuphilWork/index.php?page=legal
RewriteRule ^DuphilWork/techDivision/$ /DuphilWork/index.php?page=techDivision

RewriteRule ^DuphilWork/equipment/$ /DuphilWork/index.php?page=equipment
RewriteRule ^DuphilWork/projectImages/$ /DuphilWork/index.php?page=projectImages
RewriteRule ^DuphilWork/contact/(.*)/$ /DuphilWork/ index.php?page=contact&contactName=$1

RewriteRule ^$ /index.php
RewriteRule ^about/$ /index.php?page=about
RewriteRule ^oilfield/$ /index.php?page=oilfield
RewriteRule ^pipeline/$ /index.php?page=pipeline
RewriteRule ^safety/$ /index.php?page=safety
RewriteRule ^sales/$ /index.php?page=sales
RewriteRule ^contact/$ /index.php?page=contact
RewriteRule ^stateLicensures/$ /index.php?page=stateLicensures
RewriteRule ^keyPersonnel/$ /index.php?page=keyPersonnel
RewriteRule ^projectListings/$ /index.php?page=projectListings
RewriteRule ^legal/$ /index.php?page=legal
RewriteRule ^techDivision/$ /index.php?page=techDivision

RewriteRule ^equipment/$ /index.php?page=equipment
RewriteRule ^projectImages/$ /index.php?page=projectImages
RewriteRule ^contact/(.*)/$ /index.php?page=contact&contactName=$1