How do you write an htaccess RewriteRule to make my urls work like youtube?

家住魔仙堡 提交于 2020-05-28 12:05:33

问题


I have a php file called index.php, in this file I have a php variable $video which receives its information from a $_GET["v"] request.

If I use https://www.example.com/index.php?page=video&v=UnIq3Id I can fill the $video variable.

When I write a RewriteRule, which I have tried;

#FIRST RULE TRIED
RewriteRule ^watch$ index.php?page=video [QSA]

#SECOND RULE TRIED
RewriteCond %{QUERY_STRING} ^v=(.+)
RewriteRule ^watch /index\.php?page=video&v=%1 [QSA,R=301,L]

I got these rules from mod_rewrite with ? in the original URL like YouTube.

My $video variable is returning "watch". I would like to make my URL look like this https://www.example.com/video/watch?v=UnIq3Id.

What would be the htaccess rule to make this work?

I would also like some advise about if the following rules would be interfering;

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

My current URL due to these rules looks like this;

https://www.example.com/video/1 (1 being the video id currently which I will be changing).

My rewrite engine is working and is switched on. I know this because;

(1) I have RewriteEngine On in my htaccess file.

(2) When I go to example.com/video the page directs fine.

I ANSWERED MY OWN QUESTION

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

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

(2) Yes the following rule does interfere with my redirecting;

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

These answers worked for me, 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]

To get the unique IDs I used the following PHP class which I add to my database when uploading a video;

class idConv
{

    public function GetUniqueID()
    {
        return base_convert(microtime(false), 8, 36) ;
    }

}

Hope it helped someone and saved you the headache.

来源:https://stackoverflow.com/questions/61997156/how-do-you-write-an-htaccess-rewriterule-to-make-my-urls-work-like-youtube

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