简体   繁体   English

需要使用htaccess重命名URL

[英]Need to rename the URL using htaccess

I have tried a lot, but no luck. 我已经尝试了很多,但是没有运气。 My requirements are below. 我的要求如下。

  1. Need to rename URL from one to another 需要将网址从一个重命名为另一个

    ex: /checkout/idnumber/checkout_direct to /booking/idnumber/ 例如: /checkout/idnumber/checkout_direct/booking/idnumber/

    Note: id number is automatically generated 注意:ID号是自动生成的

  2. Need to get same ID number in renaming url too (id number will vary) 重命名网址时也需要获得相同的ID号(ID号会有所不同)

Please help me to solve this issue. 请帮我解决这个问题。 Thanks in Advance. 提前致谢。

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): 您在.htaccess文件中冷使用以下代码(请注意,这不是工作代码,因为如果您使用的网址为"/checkout/idnumber/checkout_direct"则意味着您已经拥有“干净的url”,为此,您有.htaccess重写规则,例如,清理.php并可能与此冲突):

RewriteEngine On
RewriteRule ^/?booking/([^/]+)/?$ /checkout/$1/checkout_direct [L,QSA]
#redirects from /checkout/*/checkout_direct to /booking/*/ where * == anything

If you haven't set any .htaccess rule, your urls will look like: 如果您未设置任何.htaccess规则,则您的网址将如下所示:

/checkout.php?id_number=123&checkout_direct=true

instead of: / checkout/id_number/checkout (clean URL). 而不是:/ checkout/id_number/checkout (干净的URL)。

For that, usually is a better practice (and more easy) to set the params like: 为此,通常将设置以下参数作为一种更好的做法 (更容易):

/checkout.php?id_number=123

and then: 接着:

Original URL: http://www.example.com/checkout.php?id_number=1 #id_number=123 原始网址: http : //www.example.com/checkout.php? id_number=1#id_number = 123

Desired destination URL: http://www.example.com/booking/123/ 所需的目标网址: http : //www.example.com/booking/123/

.htaccess syntax: .htaccess语法:

RewriteEngine on
RewriteCond %{QUERY_STRING} id_number=1
RewriteRule ^checkout\.php$ /booking/? [L,R=301]

Here a very good examples of how working with .htaccess: https://gist.github.com/ScottPhillips/1721489 这里是如何使用.htaccess的一个很好的例子: https : //gist.github.com/ScottPhillips/1721489

Hope it helps to you! 希望对您有帮助!

UPDATE 更新

Acording to your comment, I guess your file is 'index.php', right? 根据您的评论,我猜您的文件是“ index.php”,对吗? This is the RewriteRule: 这是RewriteRule:

RewriteRule ^example.com/checkout/([0-9]+)/?$/checkout_direct example.com/booking/$1

This will make all request from: example.com/checkout/{any_number}/checkout_direct redirects to example.com/booking/{any_number} . 这将使所有请求都来自:example.com/checkout/{any_number}/checkout_direct重定向到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. 我不确定您要2个。

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, ie, once in 'example.com/booking/111' should point to: 这就是您想要的问题,但是我认为您应该包括更多的RewriteRules以指出哪个文件将“捕获”请求,即,在“ example.com/booking/111”中应指向:

example.com/booking/111 => index.php?booking=111 example.com/booking/111 => index.php?booking=111

In order to catch the request and be able to $_GET the booking number and everything. 为了赶上请求,并能够$ _GET预订号和一切。 Hope it helps 希望能帮助到你

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

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