mod_rewrite with ? in the original URL like YouTube

蹲街弑〆低调 提交于 2020-06-01 03:41:55

问题


Ok I want to simulate the YouTube URL and redirect it to my real URL. Reason being is I used a random string to make video ID's so they aren't guessable by sequence. I have the links as so

http://www.mysite.com/watch?v=Dxdotx3iT1

and want to redirect to

http://www.mysite.com/index.php?page=videos&section=view&v=Dxdotx3iT1

Can't seem to figure out the mod rewrite. Also using a ? I believe tells it to do something.

Any help would be appreciated.


回答1:


You need to adjust your RewriteRule to include the query string using the [QSA] (query string attached) flag:

RewriteRule ^watch$ index.php?page=video&section=view [QSA]



回答2:


RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=videos&section=view&v=%1 [QSA,R=301,L]



回答3:


None of these answers worked for me, so in the end I found through trial and error the following worked for me;

The RewriteRule to get my URL to look like this https://www.example.com/video/watch?v=UnIq3Id follows;

RewriteRule ^video\/watch\?*$ index.php?page=video [L,QSA]

I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/UnIq3Id interfered with my redirecting;

RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2 

Simply commenting it out fixed the issue, as follows;

#RewriteRule ^/?([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)$ index.php?page=$1&v=$2

The RewriteRule to get my URL to look like this https://www.example.com/watch?v=UnIq3Id is as follows;

RewriteRule ^watch\?*$ index.php?page=video [L,QSA]

I found that the following rule I had previously set up to make my URL look like this https://www.example.com/video/ interfered with my redirecting;

RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]

Simply commenting it out fixed the issue, as follows;

#RewriteRule ^([A-Za-z0-9_-]+)/?$ index.php?page=$1 [NC]

Hope it helped someone and saved you the headache I had, people are not so forthcoming on this issue at times How do you write an htaccess RewriteRule to make my urls work like youtube?.



来源:https://stackoverflow.com/questions/4741033/mod-rewrite-with-in-the-original-url-like-youtube

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