简体   繁体   English

重定向而不更改URL

[英]redirect without changing url

I always suck at .htaccess I'm trying to have a website where all requests go though index.php but i'd like the URL to be like www.sample.com/home. 我总是很讨厌.htaccess,我试图建立一个网站,所有请求都通过index.php进行,但我希望URL像www.sample.com/home。 effectively that url would load www.sample.com/index.php?page=home 有效地将该网址加载到www.sample.com/index.php?page=home

Right now I have 现在我有

 RewriteRule ^/([a-z0-9-]+) /index.php?page=$1 [NC,PT,L]

Which doesnt work even though regexr test shows it should. 即使regexr测试表明它应该也不行。 I temporarily changed it to r=301 so i could see the redirect and noticed that www.sample.com/home is redirecting to www.sample.com/index.php?page=/index.php :( 我暂时将其更改为r = 301,这样我可以看到重定向,并注意到www.sample.com/home正在重定向到www.sample.com/index.php?page=/index.php :(

Any thoughts? 有什么想法吗?

Note I've been searching for this for a while and I can't find a solution that actually works. 请注意,我已经搜索了一段时间,但找不到有效的解决方案。 It's not like this was my first stop. 这不是我的第一站。

Change your rule to: 将规则更改为:

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]

RewriteRule ^([a-z0-9-]+)/?$ /index.php?page=$1 [QSA,L,NC]

Reference: Apache mod_rewrite Introduction 参考: Apache mod_rewrite简介

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM