简体   繁体   English

如何隐藏查询字符串并使用htaccess重写URL

[英]How do I hide query string and rewrite url using htaccess

I've done a bit of searching, and I can't quite find a solution. 我已经做了一些搜索,但找不到解决方案。 Here's what I would like. 这就是我想要的。 A user types: 用户键入:

Case 1: www.example.com/user/profile.php?alias=bob
Case 2: www.example.com/user/bob

Both should show up on the user's browser as: 两者都应在用户的浏览器中显示为:

www.example.com/user/bob

Internally, it should be going to "profile.php?alias=bob" 在内部,它应该转到“ profile.php?alias = bob”

Currently, I have the following mod_rewrite rules in my htaccess file: 当前,我的htaccess文件中具有以下mod_rewrite规则:

RewriteEngine On
#Converting alias to query
RewriteRule ^([A-Za-z0-9-]+)/?$ profile.php?alias=$1 [QSA,L]

Which works for case 2. For case 1 however, the url still shows up with the full query. 这适用于情况2。但是,对于情况1,URL仍然显示完整查询。 How do I get it to show up properly for case 1 as well? 我如何也可以在情况1中正确显示它?

Try adding the following to your htaccess file in the root directory of your site. 尝试将以下内容添加到站点根目录中的htaccess文件中。

RewriteEngine On
RewriteBase /

#if the request is using profile.php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /user/profile\.php\?alias=([^&\ ]+) [NC]
#redirect to user/[ALIAS]
RewriteRule . /user/%1 [L,R=301]

#extract alias from /user/alias
RewriteRule ^user/([A-Za-z0-9-]+)/?$ profile.php?alias=$1 [QSA,L]

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

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