需要使用 htaccess 重命名 URL [英] Need to rename the URL using htaccess
问题描述
我尝试了很多,但没有运气.我的要求如下.
I have tried a lot, but no luck. My requirements are below.
需要将 URL 从一个重命名为另一个
Need to rename URL from one to another
例如:/checkout/idnumber/checkout_direct
到 /booking/idnumber/
注意:id号是自动生成的
Note: id number is automatically generated
在重命名 url 时也需要获得相同的 ID 号(ID 号会有所不同)
Need to get same ID number in renaming url too (id number will vary)
请帮我解决这个问题.提前致谢.
Please help me to solve this issue. Thanks in Advance.
推荐答案
您在 .htaccess 文件中冷使用以下代码(请注意,这不是工作代码,因为如果您有 URL "/checkout/idnumber/checkout_direct"
表示你已经有了'clean urls',为此,你有一个 .htaccess 重写规则,例如清理 .php 并可能与此冲突):
You cold use in your .htaccess file the following code (note that this isn't a working code, because if you have the URL "/checkout/idnumber/checkout_direct"
it means that you already have 'clean urls', and for this, you have a .htaccess rewrite rules, for example to clean .php and may conflict with this):
RewriteEngine On
RewriteRule ^/?booking/([^/]+)/?$ /checkout/$1/checkout_direct [L,QSA]
#redirects from /checkout/*/checkout_direct to /booking/*/ where * == anything
如果您还没有设置任何 .htaccess 规则,您的网址将如下所示:
If you haven't set any .htaccess rule, your urls will look like:
/checkout.php?id_number=123&checkout_direct=true
而不是:/checkout/id_number/checkout
(干净的 URL).
instead of: /checkout/id_number/checkout
(clean URL).
为此,通常是更好的做法(并且更容易)设置如下参数:
For that, usually is a better practice (and more easy) to set the params like:
/checkout.php?id_number=123
然后:
原始网址:http://www.example.com/checkout.php?id_number=1#id_number=123
Original URL: http://www.example.com/checkout.php?id_number=1 #id_number=123
所需的目标网址:http://www.example.com/booking/123/
.htaccess 语法:
.htaccess syntax:
RewriteEngine on
RewriteCond %{QUERY_STRING} id_number=1
RewriteRule ^checkout\.php$ /booking/? [L,R=301]
这里有一个很好的使用 .htaccess 的例子:https://gist.github.com/斯科特菲利普斯/1721489
Here a very good examples of how working with .htaccess: https://gist.github.com/ScottPhillips/1721489
希望对你有帮助!
更新
根据您的评论,我猜您的文件是index.php",对吗?这是重写规则:
Acording to your comment, I guess your file is 'index.php', right? This is the RewriteRule:
RewriteRule ^example.com/checkout/([0-9]+)/?$/checkout_direct example.com/booking/$1
这将使来自:example.com/checkout/{any_number}/checkout_direct 的所有请求重定向到 example.com/booking/{any_number}.或者:
This will make all request from: example.com/checkout/{any_number}/checkout_direct redirects to example.com/booking/{any_number} . Or:
RewriteRule example.com/booking/([0-9]+)/?$ ^example.com/checkout/$1/checkout_direct
我不确定您想要哪两个.
I'm not really sure what of 2 you want.
这是您在问题中想要的,但我认为您应该包含更多 RewriteRules 以指出哪个文件将捕获"请求,即,一旦在example.com/booking/111"中应指向:
This is what you want in your question, but I think that you should include more RewriteRules to point out which file will 'catch' the request, i.e, once in 'example.com/booking/111' should point to:
example.com/booking/111
=> index.php?booking=111
为了捕获请求并能够 $_GET 预订号和所有内容.希望有帮助
In order to catch the request and be able to $_GET the booking number and everything. Hope it helps
这篇关于需要使用 htaccess 重命名 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!