Need to rename the URL using htaccess

只谈情不闲聊 提交于 2019-12-12 03:34:52

问题


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:


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



来源:https://stackoverflow.com/questions/36332977/need-to-rename-the-url-using-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!