2011-05-02 39 views
1

我有以下页面:www.domain.com/index.php?route=information/contact,我想改写它,这样它显示为:www.domain.com/contact,但还有更多......如何重写URL以便可以将重写输入到URL栏中?

什么是重要的,就是当在www.domain.com/contact有人类型,它重定向他们到www.domain.com/index.php?route=information/contact,这又被重写为www.domain.com/contact

我感谢任何帮助!谢谢。

编辑:澄清

我希望用户能够进入www.domain.com/contact和被重定向到www.domain.com/index.php?route=information/contact

然而,一旦重定向,我想一个纯粹的审美改写使www.domain.com/index.php?route=information/contact显示为www.domain.com/contact(同他们在键入。)

这可能吗?

编辑:目前我的.htaccess文件...

Options +FollowSymlinks 

# Prevent Directoy listing 
Options -Indexes 

# Prevent Direct Access to files 
<FilesMatch "\.(tpl|ini)"> 
Order deny,allow 
Deny from all 
</FilesMatch> 

# SEO URL Settings 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)\?*$ index.php?_route_=$1 [L,QSA] 

RewriteCond %{QUERY_STRING} ^route=common/home$ 
RewriteCond %{REQUEST_METHOD} !^POST$ 
RewriteRule ^index\.php$ http://www.domain.com/? [R=301,L] 


### Additional Settings that may need to be enabled for some servers 
### Uncomment the commands by removing the # sign in front of it. 
### If you get an "Internal Server Error 500" after enabling, then restore the # as this means your host 

doesn't allow that. 

# 1. If your cart only allows you to add one item at a time, it is possible register_globals is on. This 

may work to disable it: 
# php_flag register_globals off 
+0

你描述将创建一个无限重定向什么... 重定向联系? - >的index.php路线=信息/接触 重定向的index.php路线=信息/ contect - >接触 确定这就是你想要做的? – 2011-05-02 01:32:17

+0

@David - 我只希望'index.php?route = information/contact'被重写(不重定向)......只是纯粹的美学 - 但关键是人们需要能够输入审美URL ,并将其重定向到真实网址......这将(仅在外观上)与审美网址相同。 – jlmakes 2011-05-02 02:08:28

+0

但是审美的URL将不得不重写。为什么不使用功能性URL来运行脚本? – 2011-05-02 02:16:44

回答

1

在.htaccess文件请尝试以下规则:

Options +FollowSymlinks -MultiViews 
RewriteEngine on 

RewriteCond %{THE_REQUEST} ^GET\s/+index\.php [NC] 
RewriteCond %{QUERY_STRING} ^route=information [NC] 
RewriteRule . /warranty? [L,NC,R=301] 

RewriteRule ^warranty$ /index.php?route=information/contact [L,NC] 

L将确保用户在浏览器的URL不会改变,重定向发生内部

+0

@anubhava - 它看起来像你截断网址...上面只是例子,实际重写需要指向'www.domain.com/index.php路线=信息/ contact'为'www.domain? .com/warranty“...因为联系表单实际上是用于注册产品保修 - 您示例中的代码将如何更改?我没有想到“联系”和“信息/联系”的混淆/相似性。 – jlmakes 2011-05-02 04:53:21

+0

编辑上面的回答,以反映'的index.php?路径=信息/ contact'到'/ warranty'。由于原来的URI这里没有提及目标URI'/ warranty'因此每条规则一样,你必须添加1个重写规则线在我的答案。 – anubhava 2011-05-02 04:57:07

+0

@anubhava - 嗯,我弹出那里的代码...键入在www.domain。com/warranty“,并没有发生任何事情。我手动输入了“www.domain.com/index.php?route = information/contact”,但URL栏未更改。难道我做错了什么? – jlmakes 2011-05-02 05:02:13

1

你提的问题是非常不清楚,我怀疑是经验不足惹的祸。

随着以下规则:

RewriteRule /?(.*) index.php?route=information/$1 

位置栏将读“/接触”,但index.php为经由内部重写被调用。

用小修改:

RewriteRule /?(.*) index.php?route=information/$1 [R] 

位置栏将读取“/index.php?route=information/contact”和index.php的将被调用,重定向之后。

一如既往,规则应遵循适当的RewriteCond,以避免在请求实际文件时重写。

+0

@Ignacio - 查看我对Anubhava答案的评论。我对这种混淆表示歉意 - 我不认为人们会试图截断网址以显示“联系人”。理想情况下,“www.domain.com/whatever-I-pick”会指向“www.domain.com/index.php?route = information/contact”,同时离开URL栏显示“www.domain.com/whatever-I -pick'。 – jlmakes 2011-05-02 04:56:02

+0

然后使用前一条规则,根据情况替换LHS。 – 2011-05-02 05:02:09

+0

条件是这样吗? 'RewriteCond%{QUERY_STRING}^route = information/contact $'另外,什么是LHS?我试着用你提供的第一行代码,它杀死了我的CSS – jlmakes 2011-05-02 05:04:56

1

AFAIK,您不能让地址栏显示与加载页面的地址不同的地址。如果您希望用户在查看页面时在地址栏中看到www.domain.com/contact,则需要在请求该URL时使服务器实际返回页面内容(而不是重定向)。

我想你可能会误解URL重写:它不是为了改变用户在地址栏中看到的内容,而是改变当用户请求到达时看到的服务器。如果您创建的重写规则将/foo更改为/bar,则当用户在浏览器中键入/foo时,服务器会将其视为/bar的请求。

你想要的是,当用户在浏览器中键入www.domain.com/contact时,服务器应该将其视为www.domain.com/index.php?route=information/contact的请求,但浏览器仍应显示用户输入的漂亮URL。要做到这一点的方法是简单地在服务器上重写/contact/index.php?route=information/contact。不需要重定向;用户只是简单地请求漂亮的URL,服务器根据相应的丑陋处理请求并返回结果页面。