且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Apache重写时隐藏URL的变化?

更新时间:2023-02-24 09:34:39

第一条规则将重定向你的丑陋的URL到pretty的URL格式。

First rule will redirect your ugly URL to the pretty URL format.

第二条规则将在内部重定向回用户将无法看到丑陋的URL。

Second rule will internally redirect it back for the user will not see the ugly URL.

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

# Redirect /page.cfm?pagevar=abc123 to /Page/abc123
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page\.cfm\?pagevar=([^&\s]+) [NC]
RewriteRule ^ /Page/%1? [R=301,L]

# Internally forward /Page/abc123 to /page.cfm?pagevar=abc123
RewriteRule ^Page/(.*)/?$ /page.cfm?pagevar=$1 [QSA,NC,L]

以上规则是要在的.htaccess 文件中使用,并假定 page.cfm 是根您正在访问的文件夹随的.htaccess 文件。

The above rules are to be used on .htaccess files and assumes page.cfm is on the root of your domain folder along with the .htaccess file.

就像你的例子提出。