需要使用htaccess重命名URL [英] Need to rename the URL using htaccess

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

问题描述

我尝试了很多,但是没有运气.我的要求如下.

I have tried a lot, but no luck. My requirements are below.

  1. 需要将URL从一个重命名为另一个

  1. 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",则意味着您已经具有干净的网址",为此,您具有.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/

Desired destination URL: 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/ScottPhillips/1721489

Here a very good examples of how working with .htaccess: https://gist.github.com/ScottPhillips/1721489

希望对您有帮助!

更新

根据您的评论,我想您的文件是'index.php',对吗?这是RewriteRule:

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

我不确定您要2个.

这是您想要的问题,但是我认为您应该包括更多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屋!

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