在htaccess中使用重写的友好URL [英] Friendly URLs using rewrite in htaccess

查看:65
本文介绍了在htaccess中使用重写的友好URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个网址

http://mysite.com/profile?id=78

我想将其更改为这样

http://mysite.com/78/

我该怎么做?

我的htaccess中有此代码,但似乎不起作用.

I have this code in my htaccess, but it seems it's not working.

RewriteRule ^id/([a-zA-Z0-9]+)/$ profile?id=$1

请在这方面指导我.

您的帮助将不胜感激和奖励.

Your help would be greatly appreciated and rewarded.

谢谢!

推荐答案

我非常确定您应该使用RewriteBase指令

I am pretty sure that you should use the RewriteBase directive

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^u/(!(profile.php))$ u/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^u/(!(profile.php))/$ profile.php?name=$1 [L]

这会将http://mysite.com/u/john/重写为http://mysite.com/profile?name=john.

编辑,这是新答案

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^u/(!(profile.php))$ u/$1/ [R=301,L]
# The following rule does the rewrite.
RewriteRule ^u/(!(profile.php))/$ profile.php?name=$1 [L]
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ u/%1? [R=301,L]

这篇关于在htaccess中使用重写的友好URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆