url redirecting using .htaccess

泄露秘密 提交于 2020-01-04 14:29:32

问题


I need your help to make some url redirection using .htaccess The case is that I have url like www.website.com/index.php?chID=1234 which opens particular tv channel on the website. The problem is that I want to make these urls like www.website.com/channel-name/1234 (where 1234 is that chID variable, to get record from the database, of course without it would be more nice). here is the working link - http://www.livepage.info/world-tv-channels.php?chID=1512

any suggestion will be greatly appreciated! Thank you!


回答1:


To redirect all channels, you could use:

RewriteEngine On
RewriteBase /
RewriteRule ^([^/]+/\d+)/$ http://www.livepage.info/$1 [L,R=302,QSA]
RewriteRule ^[^/]+/(\d+)$ index.php?chID=$1 [L,QSA]

Don't forget that you will need a new HEAD tag in your HTML (otherwise images will be fetched from http://www.livepage.info/euronews-russia/images/...):

<base href="http://www.livepage.info/">

Of course, you could use .htaccess instead. This is, however, not as good as the base href tag, since the client wouldn't use the cache when viewing different channels, or if the client gets redirected, will need two requests for each image etc.

RewriteRule ^([^/]+)/(images|folder2|folder3)(/.+)$ $2$3 [L]



回答2:


RewriteEngine On
RewriteBase /

RewriteRule ^channel-name/(\d+)/?$ index.php?chID=$1 [L]

This will redirect URLs in the format channel-name/chID with an optional trailing slash to the raw URL you provided. chID must be 1 or more digits.



来源:https://stackoverflow.com/questions/6063598/url-redirecting-using-htaccess

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