简体   繁体   中英

htaccess shorten URL

I have a URL like this:

http://www.example.com/folder1/assets/Video/150/filename.mp4

I'm trying to shorten it like this

http://www.example.com/challenge/Video/150/filename.mp4

My current .htaccess implementation is this:

RewriteEngine on
RewriteRule ^/challenge/Video$ /folder1/assets/Video [L]

But it's not working any assistance would help, I've tried other variations that I've seen but apparently, I'm doing something wrong.

EDIT 1)

To help give more insight: The user uploads a video to the server, and then the user is able to see/watch user submitted videos from the server. When the user submits the video the url is:

http://www.example.com/folder1/assets/Video/150/filename.mp4

When the user retrieves the video, I wanted them to get this URL http://www.example.com/challenge/Video/150/filename.mp4

to hide the actual file path/directory.

Edit 2)

I just added a picture from the results of the changes that @arkascha suggested, after fixing the PHP errors, i'm getting this when i change [END] to [L], when I use [END] I get a 500 error:

在此处输入图片说明

Edit 3) This is my full HTACCESS code:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com
RewriteCond /var/www/example.com/%1 -d
RewriteCond %{ENV:REDIRECT_STATUS}  =""
RewriteRule ^(.*)$ /%1/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

RewriteRule ^/?challenge/Video/(\d+)/(.+)$ 
\/WebServices/assets/Video/$1/$2 [L]

When I removed everything above and just left the rewrite code you gave me it worked, but, i'm sure I may have needed to have some of these there. So if someone can help direct me to what line is causing it to act funny. I think it's the:

RewriteRule ^(.*)$ index.php?/$1 [L]

but i'm not 100% sure. Thanks

This probably is what you are looking for:

RewriteEngine on
RewriteRule ^/?challenge/Video/(\d+)/(.+)$ /folder1/assets/Video/$1/$2 [END]

In case you encounter an "internal server error" (http status 500) using that rule chances are that you operate a very old version of the apache http server. Have a try replacing the [END] flag with a [L] flag in that case. Your http servers error log file will have a specific for that issue.

The above internal rewriting rule will work in the http servers host configuration and likewise in dynamic configuration files (".htaccess" style files).

A general hint on that: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files ( .htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM