Need to rename the URL using htaccess

2019-09-08 10:37发布

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/

    Note: id number is automatically generated

  2. Need to get same ID number in renaming url too (id number will vary)

Please help me to solve this issue. Thanks in Advance.

1条回答
beautiful°
2楼-- · 2019-09-08 11:14

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

If you haven't set any .htaccess rule, your urls will look like:

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

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

and then:

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

Desired destination URL: http://www.example.com/booking/123/

.htaccess syntax:

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

Hope it helps to you!

UPDATE

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

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.

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

In order to catch the request and be able to $_GET the booking number and everything. Hope it helps

查看更多
登录 后发表回答