i have some links
example.com/1.mp4
example.com/2.mp4
is there a way to block accessing directly to those files but allow just if u add this
example.com/1.mp4?token=12345
so all mp4 files can be access only if u add ?token=12345 at end
so without it
RewriteCond %{QUERY_STRING} !^token=12345
RewriteRule ^.* - [F,L]
i tried this but is not working, so i want those mp4 to be played on vlc or any other player just if ?token=12345 is presend otherwise redirect to any other video
What you had should work. Try this:
RewriteEngine on
RewriteCond %{QUERY_STRING} !=token=12345
RewriteRule \.mp4$ - [F,L]
That will return forbidden on .mp4 files unless token=12345 is present. As for redirecting to a video, you could do that with:
RewriteEngine on
RewriteCond %{QUERY_STRING} !=token=12345
RewriteRule \.mp4$ /video [R=301,L]
Replacing /video with the path to the video. Make sure it's not .mp4 or you'll get a loop.
Let me know any problems.
Related
The website I'm working on is using some cms. I need to add a static website to this. When I put mypage.html in the main directory and go to www.website.com/mypage.html it works. I would like the page to be accessible without '.html' ending. I experimented with editing htaccess files but always end up with error of too many redirections.
What I entered were various combinations, for example
Redirect 301 http://website.com/mypage http://website.com/mypage.html
The htaccess file I'm using looks like this:
:Location /*.php
Use php54
:Location
RewriteEngine On
DirectoryIndex index_prod.php
Options -Indexes
RewriteRule ^.*\.(css|png|swf|js|gif|jpeg|jpg|flv|pdf|doc)$ - [L]
RewriteRule ^net2ftp - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^/?$ plug.html [L]
#RewriteCond %{REQUEST_URI} !=/
RewriteRule ^/?.* index_prod.php
I'm looking for tips or to be explicitly told what and where to put in htaccess file to make it work (if it's possible)
Could you please try following, considering that you want without extension file URLs to be served by html extension files. Also since you didn't mention any specific condition before RewriteRule hence that redirection errors are coming to it, because its keep on redirecting in lack of any condition/check's presence(till its maximum redirection limit is crossed).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ /$1.html [NC,L]
i want to access videos in my site example:
live.example.com/video1/1.mp4?token
so if i dont add ?token it will redirect to redirect.mp4
RewriteEngine on
RewriteCond %{QUERY_STRING} !=token
RewriteRule \.mp4$ http://live.example.com/redirect.mp4 [R=302,L]
i use this code but how to make it just for those folders:
video1, video2, video3
so what i want is to make rule just for mp4 files that are on video1, video2, video3 folder so they will play just if i add ?token at end of .mp4
You can change it to this:
RewriteEngine on
RewriteCond %{QUERY_STRING} !=token
RewriteRule ^(?:video1|video2|video3)/[^/]+\.mp4$ http://example.com/redirect.mp4 [R=302,L]
On the start i want you to take notice, i did try finding solution in google, or in stackoverflow. I found some tips, but they didn't work for me, or i simply failed following instructions.
So, here is my site directories:
www.domain.pl/index.php <--index file
www.domain.pl/images/ <--images folder
www.domain.pl/styles/ <--styles folder, for now just 1 css file
www.domain.pl/script/ <--scripts folder, js files, and 1 php file called with include
www.domain.pl/font/ <--font files
Now, when i open my website with www.domain.pl or www.domain.pl/index.php it works like a charm. But i want to be able to add some parameter for example www.domain.pl/index.php?action=dosmth, but it doesn't look good for a users, so thats the place where I wanted to use mod rewrite.
Now comes tricky part, i want it to look like www.domain.pl/index/actionparam/ but when i do that, I get error in console, saying my images, scripts, and css cant be loaded/found. (i tried rewriting it like that: RewriteRule ^index/([^-]+)/script/jquery-1.7.1.min.js$ script/jquery-1.7.1.min.js [L] but I cant use it for every single image, script, css, or font file. There need to be better way.
So, my mod rewrite so far:
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.pl/$1/ [L,R=301]
RewriteRule ^index.html$ index.php [L]
RewriteRule ^index/$ index.php [L]
RewriteRule ^index/([^-]+)/$ index.php?action=$1 [L]
First part, i am trying to add "slash" at the end of url, if user didnt put it there.
http://domain.pl/index->http://domain.pl/index/ or
http://domain.pl/index/myaction->http://domain.pl/index/myaction/
Question
Is it possible to somehow skip folders with images, scripts etc when rewriting it? Or how could i rewrite all images at once?
Additional question
http://domain.pl/myaction/ redirect straight to http://domain.pl/index.php?action=myaction right away, without need of putting /index/ in there? Yes, i am learning mod rewrite.
You don't have to include index in the request, only the parameter. Example:
Request: http://domain.pl/myaction
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# Exclude existing files and directories.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
# Get the parameter and pass it to index.php
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/? /index.php?action=$1 [L,NC]
I want to mask the URL http://example.com/index.php?path=controller/function to http://example.com/controller/function. function isn't required to be present.
How can I do this with an htaccess file?
The following isn't working.
RewriteRule ^/?assets/(.*)$ assets/$1 [L] # directory for CSS, images, and JavaScript
RewriteRule ^$ /index [redirect]
RewriteRule ^([a-zA-Z]+)/?([a-zA-Z/]*)$ index.php?path=$1/$2 [L]
Currently, if I enter http://example.com/controller/function in the browser, I receive a 404 error, but entering http://example.com/index.php?path=controller/function works.
Thanks!
You may try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)(.*)?$ index.php?path=$1$2 [L]
It maps internally
http://example.com/var1/var2
To:
http://example.com/index.php?path=var1/var2
Keeps the incoming trailing slash behavior and var2 is optional.
At the moment I'm using the following HTACCESS code
RewriteEngine On
# Lose the www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
# Clean profile URLs
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ profile.php?user=$1
So the links do work when I go to http://mysite.com/username, I see the profile. But I can also still use the http://mysite.com/profile.php?user=username.
What I want is that that second on is being directed to the short version of http://mysite.com/username.
Another problem is that when I surf to http://www.mysite.com/username, it wil rewrite the url as http://mysite.com/profile.php?user=username.
Also, how can I avoid this for certain folders like my images folder? If i go to the images folder I get http://mysite.com/images/?user=images
Also if there is a slash behind the clean url, the page acts weird.
If the site is not yet live I would not worry about the /profile.php?user=username urls. Since people will have to know that there is a php file names profile.php. If you never create any links in your html to profile.php directly, no one will know.
If the site is live already, let us know.
As for the images problem.
RewriteCond $1 !^images/
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ profile.php?user=$1
to also do a css and/or js folder
RewriteCond $1 !^(images|css|js)/
RewriteRule ^([a-zA-Z0-9_-]+)(/?)$ profile.php?user=$1
The last problem of the tailing slash most likely has to do with the use of relative urls in your html. Try replacing href="path/resource.ext" with href="/path/resource.ext". Same for src="..." etc.
You need to add a Last action on the change url rule. I changed my code to the following:
# Lose the www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
# Exclude existing files from redirect
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.* - [L]
# Clean profile URLs
RewriteRule ^([^/]*)\/$ http://businessgame.be/$1 [R=301]
RewriteRule ^([^/]*)(/?)$ profile.php?user=$1
This seems to work great.