I'm tried to protect my mp3 using mod_rewrite in my htaccess file.
I read this threads:
how to write a .htaccess redirect like stackoverflow does for its questions
htaccess rewrite dynamic mp3 to dynamic php?
How to hide filename from url by using .htaccess
this website:
https://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
and this video:
https://www.youtube.com/watch?v=uPlZekNEU60
I was able to rewrite filename to filename.pdf with this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.pdf -f
RewriteRule ^([^\.]+)$ $1.pdf [NC,L]
Now, to protect my mp3 I want to rewrite my url
filename.mp3/?uuid=something to https://cdn.com/123456/t/filename.mp3
I tried this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.mp3 -f
RewriteRule ^([a-zA-Z0-9]{30})$ https://cdn.com/123456/t/$1.mp3 [NC,L]
but it doesn't work
I tried this one:
RewriteRule ^(\w*.mp3)+)$ https://cdn.com/123456/t/$1 [NC,L]
but it puts the website down :(
Any tip?
Your last RewriteRule has a closing parenthesis too much at the end and will repeat matching mp3 1 or more times.
If you remove that parenthesis leaving ^(\w*.mp3)+ it will still repeat one or more times a grouping structure that will match this pattern \w*.mp3. That will match 0+ times a word character followed by any character due to the dot and mp3.
That could for example match zmp3. If you want to match a literal dot you have to escape it.
I think that you meant:
RewriteRule ^(\w+\.mp3/)$ https://cdn.com/123456/t/$1 [NC,L]
You might check if the querystring consist of a single key/value pair:
RewriteCond %{QUERY_STRING} ^uuid=\w+$ [NC]
Related
I have hundreds of these old links I need to redirect.
Here is one example:
/index.php?option=com_content&view=article&id=433:seventh-character-code-categories-and-icd-10-cm&Itemid=101&showall=1
to
/seventh-character-code-categories-and-icd-10-cm
Essentially I need to remove the /index.php?option=com_content&view=article&id=433: part.
I tried this but I am getting confused with the [0-9] and : parts, so the following does not work:
RewriteRule ^/index.php?option=com_content&view=article&id=[0-9]:(.*)$ /$1 [L,R=301]
Say you want to capture from after : to right before & in the query string you mentioned, then try this expression:
^[^\:]*\:([^\&]*)\&.*$
As #starkeen mentioned in comments, you got to check against the query string. This can be done using RewriteCond %{QUERY_STRING}
So if index.php is in the root folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ http://example.com/%1 [R=301,L]
Here's another example. This one is for a sub folder:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/pages\/index\.php$
RewriteCond %{QUERY_STRING} ^[^\:]*\:([^\&]*)\&.*$
RewriteRule ^(.*)$ /pages/%1? [R=301,L]
Also, notice the ? at the end of the url /pages/%1?, this prevents from re-attaching the query string.
Another thing, captured groups will be set to variables %{number} since set in the RewriteCond.
BTW, depending on your server's configuration, you may need to add the NE flag, like [NE,L,R=301] Plus test whether it is necessary to double escape the literal characters.
what is about direct approach. Skip all till semicolon, mach string till & and replace all with first much
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} [^:]+:([\w-]+[^&]).*
RewriteRule .*$ \/%1? [R=301,L]
</IfModule>
I am trying to add trailing slash to a url. I have my own logic to do so but found another one on stackoverflow (here). Now the regex in this line
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
apparently matches the RewriteRule for the url http://www.example.com/wp-admin.
What I first did was:
RewriteCond %{REQUEST_URI} /wp-admin$ [NC]
RewriteCond %{REQUEST_URI} !/+$
RewriteRule ^ %{ENV:proto}://%{HTTP_HOST}%{REQUEST_URI}/ [R=301,L]
So my question is how /wp-admin$ is similar to ^([_0-9a-zA-Z-]+/)?wp-admin$?
This is because leading slash is matched when you use RewriteCond %{REQUEST_URI} but current directory (relative to DocumentRoot) is stripped when you use a pattern in RewriteRule in .htaccess file, which is a per directory directive.
Hence a leading slash is not matched in RewriteRule but is matched in RewriteCond.
So I have a legacy site archived with 1000s of image URLs like
img/2265/thumb/Incubator.jpg?1344891444
and the file it points at is sitting at:
img/2265/thumb/Incubator-1344891444.jpg
some of the files are jpegs, pngs etc, but they all follow this pattern of a hyphen then querystring value then extension. (keeping rest of path the same)
So far I have got the Querystring part but cannot figure out how to combine this with a RewriteRule (?) to keep the filename but add in the matched value.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([0-9]*)$
RewriteRule PATTERN_TO_MATCHJPG|PNG|GIF$ SOMETHING-$1.jpeg [L]
Is it that the RewriteRule first part should be a rewritecond? :-/
thanks for any tips.
UPDATED to show answer in nice format!**
RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule (.+?)\.(png|gif|jpg|jpeg|JPG|JPEG|PNG|GIF)$ $1-%1.$2? [L]
You can use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^([0-9]+)$
RewriteRule ^(.+?)\.(png|gif|jpe?g)$ /$1-%1.$2? [L,NC]
I would like clients to be able to access certain files using an arbitrary version number to bypass caching.
For example, suppose there are two files: styles.css and jquery.min.js They should be able to request either the originals or styles.23.css and jquery.min.5039.css.
The rule I came up with was:
RewriteEngine On
RewriteRule ^(.*)\.(?!\..*)[\d]+\.(.*)$ $1.$2 # strip out version number
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
Broken down, here's my thought on what it should be doing:
^(.*) – starting from the beginning, match all
\. – up to the first period...
(?!\..*) - ...which is not followed by a period and anything,
[\d]+\. – then match if ends in one or more digits followed by a period...
(.*)$ – ...and anything
This RegEx actually works seems to work in PHP but not .htaccess, which has me a bit confused.
Thank you in advance.
Why do you need lookahead etc. Following should work:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)\.\d+\.(.*)$ $1.$2 [L]
My htaccess is located in localhost/me/.htaccess, and I want to append anything after the last / with .php
e.g. localhost/me/test/index rewrites to localhost/me/test/index.php
So far RewriteRule ^([A-Za-z]+)$ $1.php [NC,L] works for localhost/me/index, but I can't get it working for the first example up there. Why doesn't ^/([A-Za-z]+)$ /$1.php [NC,L] work, and how do I change it to work?
Use this rule:
# add .php file extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
It will check if such .php exist before rewriting
The main problem you had is your pattern: ^([A-Za-z]+)$ it will match index but not test/index as it only allows a-z characters. You would need to add / into a pattern: ^([a-z/]+)$.
because you have [NC] flag (no case matching), there is no need to have both A-Z and a-z
I'm using more global/general pattern (.+)-- it will match any characters and because it comes with "check if file exist" conditions, there is no need to worry about limited set of characters.
It doesn't work because you're matching only on alpha letters and you don't have a / in the character class, but your URI is me/test/index . Try this:
RewriteEngine On
RewriteRule ^([A-Za-z/]+)$ $1.php [NC,L,QSA]
Also, since you're using [NC], you really only need a-z rather than A-Za-z but it doesn't hurt anything.