User avatar
WillBoxOwO
Posts: 77
Joined: Wed Jun 27, 2018 10:24 pm

[Apache] Clean links?

Thu Nov 29, 2018 1:13 am

Hello.
I am making a NAS on my little Pi. :)
Here is my problem:

I want to stream video files on there, but the links generated (path to videos) are quite messy.
Here is an example video file:
Image

As you can see, it contains spaces.
But the link generated looks like this:
Image

How could I replace all the "%20" by "-" without renaming the file :shock: ?

Thanks for the help.
I like cats

User avatar
WillBoxOwO
Posts: 77
Joined: Wed Jun 27, 2018 10:24 pm

Re: [Apache] Clean links?

Thu Nov 29, 2018 2:04 am

I found this but I dont understand anything :lol:
http://httpd.apache.org/docs/2.0/mod/mo ... ewriteRule
I like cats

ghans
Posts: 7819
Joined: Mon Dec 12, 2011 8:30 pm
Location: Germany

Re: [Apache] Clean links?

Thu Nov 29, 2018 6:19 am

You must rename files on the server if it bothers you this much.
This process is called "precent encoding" and your browser is just doing its job by using it.

ghans
• Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings.
• Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org

bzt
Posts: 210
Joined: Sat Oct 14, 2017 9:57 pm

Re: [Apache] Clean links?

Thu Nov 29, 2018 12:37 pm

Hi,

Rewrite is to match an url an redirect it to another. It's not particularly good for replacing arbitrary number of characters in the url. Also that would mean you cannot have hypens in the filenames, but maybe that's okay.

One workaround would be to pass the url as an argument to a script, and replace spaces there. It has a downside that it's much more resource demanding (you must have a script interpreter (php for example), and all the file must be proxied through the script (requires memory and slow, also there are serious security implications) or you have to set up a sendfile module).

Code: Select all

RewriteRule ^(.*)$ index.php?q=$1

Code: Select all

die(file_get_contents(str_replace("-", " ", $_GET['q'])));

Another, little bit uglier, but more effective and much more secure solution would be to create more rewrite rules. One for each hypen, only if you know the maximum number of spaces in a filename in advance:

Code: Select all

RewriteRule ^([^\-]+)[\-]([^\-]+)[\-]([^\-]+)[\-]([^\-]+)$ $1%20$2%20$3%20$4
RewriteRule ^([^\-]+)[\-]([^\-]+)[\-]([^\-]+)$ $1%20$2%20$3
RewriteRule ^([^\-]+)[\-]([^\-]+)$ $1%20$2
This splits the url into several hypen-less character blocks ("([^\-]+)"), and then concatenates those matches with an url encoded space ("$x"+"%20"). The first rule matches 3 hypens, the second 2, the last one etc. You have as much rules as the maximum number of spaces.

Finally, as suggested before, the best solution would be to write a small shell script that renames the files (use find -exec), and you run that every time a new file is uploaded.

Cheers,
bzt

User avatar
WillBoxOwO
Posts: 77
Joined: Wed Jun 27, 2018 10:24 pm

Re: [Apache] Clean links?

Thu Nov 29, 2018 11:28 pm

bzt wrote:
Thu Nov 29, 2018 12:37 pm

Code: Select all

RewriteRule ^(.*)$ index.php?q=$1

Code: Select all

die(file_get_contents(str_replace("-", " ", $_GET['q'])));
Thanks for the quick reply! Whats so "unsecure" about this method? And where should I put the code..? I tried putting it in the .htaccess file located at /var/www/html but it gave the the infamous "Error 500" error when trying to access the page. Should I put only the first line or both? :lol: Im kinda confused here. 8-) Anyways thanks alot for the help.
I like cats

User avatar
WillBoxOwO
Posts: 77
Joined: Wed Jun 27, 2018 10:24 pm

Re: [Apache] Clean links?

Fri Nov 30, 2018 8:32 pm

To clean up a bit, I used the following:

Code: Select all

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.mp4 -f 
RewriteRule ^(.*)$ $1.mp4
It removes the .mp4 extension from my files. Apache still redirected me to "my_video.mp4" in the autoindex when clicking on links, witch give me a 404-error, so I added this to the footer of my auto-index page:

Code: Select all

<script>
document.body.innerHTML = document.body.innerHTML.split(".mp4").join("")
</script>
It removes all of the automaticly generated .mp4 extensions in my page code- witch give me clean links like "/home/stream/my_video".
My initial problem isnt out of the way tho- I want my links to look like "/home/stream/my_video" but I want people to see it in the autoindex page as "My Video".

I tried this:

Code: Select all

document.body.innerHTML = document.body.innerHTML.split("_").join(" ")
It removes all of the "_" in my page. It makes my index real clean, but it breaks the links, since its replacing everything in the code. Now, when I click a video link, it redirects me to "my%20video".

How could I modify my javascript code to make it redirect to my_video instead without making the index look crappy again (with "_" instead of spaces?)?

Cheers.
I like cats

Return to “Other programming languages”