如何重写URL以便可以在URL栏中输入重写?

时间:2011-05-02 01:18:05

标签: .htaccess mod-rewrite

我有以下页面: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/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显示为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 (与他们输入的内容相同。)

这可能吗?

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

{{1}}

3 个答案:

答案 0 :(得分:1)

你的问题非常不清楚,我怀疑缺乏经验是罪魁祸首。

遵守以下规则:

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

位置栏将显示“/ contact”,但index.php将通过内部重写调用。

稍加修改:

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

位置栏将显示“/index.php?route=information/contact”,重定向后将调用index.php。

与往常一样,规则应遵循相应的RewriteCond,以避免在请求实际文件时重写。

答案 1 :(得分:1)

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

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

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

答案 2 :(得分: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将确保浏览器中用户的网址不会更改并且重定向发生在内部

相关问题