I can't figure this one out no matter how many times I google it or think about it. I have a RewriteRule in my .htaccess file: RewriteRule ^/download/([^/\.])/?$ /downloadfile.php?f=$1 [L]
When I use this, my page loads fine, but going to the link http://www.example.com/download/file.ext, it pulls a 404 page. However, if I load the page, then change my RewriteRule to RewriteRule ^/download/([^/\])/?$ /downloadfile.php?f=$1 [L] (noting the RegEx change), the link works exactly how I expect it to... Until I reload the page, which results in a 500 error because of a bad regex expression? (I checked my Apache error log, thats how I know it reads as a bad regex)
So, what can I do to make this work? I've tried (.*) and ([.*]) for regex as well, that didn't work either.. can anyone tell me what I'm doing wrong?
mod_rewrite strips out the prefix (the leading slash) from the URI when you use it in an .htaccess file. Your regex needs to have it removed:
RewriteRule ^download/([^/\.]*)/?$ /downloadfile.php?f=$1 [L]
This should work:
RewriteRule ^/?download/([^/]*) /downloadfile.php?f=$1 [L]
The leading slash is required in apache 1.x and stripped in version 2.x
You were also denying the dot character.
Related
Im trying to redirect a bunch of support files to a different support system except for any files that have the folder software_updates in them. the following is the rule that I wrote.
RewriteRule ^/support/.*(?!software_updates).*$ newurl_location [NC,L,R=301]
this excludes /support/software_updates/ but not /support/product/software_updates Im trying to exclude any URL that has software_updates anywhere in the URL after support.
Try:
RewriteRule ^/support/(?!.*software_updates) newurl_location [NC,L,R=301]
I don't have Apache handy to test it, but it should work.
I have tested this and believe that it is what you're looking for (please note the changes in the / and the .* )
RewriteRule ^support/(?!.*?software_updates) newurl_location [NC,L,R=301]
You were nearly there.
First off, you don't need the initial /
You don't want .*(?!software_updates).*, because this will match software_updates. Why? The dot-star eats the whole string, then, at the end, you have the assertion that software_updates is not next. And of course this is true.
This should do the trick.
RewriteRule ^support/(?!.*software_updates).*$ newurl_location [NC,L,R=301]
I changed one of my rewrite rule with new structure and now I'm notice error 404 in Google web master tools to all of my pages.
Old rewrite rule:
RewriteRule ^channel.([a-zA-Z0-9-_]+)-([a-z]+)$ channel.php?user=$1&q=$2
New rewrite rule:
RewriteRule ^channel:([a-zA-Z0-9-_]+)$ channel.php?user=$1 [L]
Now I'm getting a lot of error 404 for all pages with old rewrite rule. I got custom 404 page and correctly return header not found, but Google still report me errors. How can I fix it?
sorry for my bad English, but really need to fix this...
Not sure about before and after effect but your regex is incorrect where you have unescaped hyphen in the middle. An hyphen needs to be at 1st or last position to avoid escaping in character class.
Use this rule:
RewriteRule ^channel.([\w-]+)-([a-z]+)$ channel.php?user=$1&q=$2 [L,QSA]
OR:
RewriteRule ^channel:([\w-]+)$ channel.php?user=$1 [L,QSA]
It would be better to know the URLs you're trying to match using these rules.
EDIT: As per comments use this rule to block old URLs:
RewriteCond %{THE_REQUEST} \s/+channel\. [NC]
RewriteRule ^channel\.([^-]+) /channel:$1 [L,R=301,NC]
I'm having some problems with a mod_rewrite rule not matching, when I think it should.
The rule is as follows:
RewriteRule ^api/(.*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
And an example url is:
http://ourdomain.tld/api/aaaaa/bbbb/cccc%0Adddd.json
This rule works fine as long as you don't have $0A in the url (as far as I can tell).
I realise that . means anything other than a new line and have tried rules like the following to get around either new lines or percentage symbols as I'm not sure quite how it is formatted at the time that mod_rewrite sees it, but none have worked:
RewriteRule ^api/([.\s]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
RewriteRule ^api/([.\%]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
RewriteRule ^api/([.%]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
Strangely even the following fails on all urls, even those without % symbols:
RewriteRule ^api/([.]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
I'd be very grateful if someone could shed some light on this for me as I cannot really go changing how the API works (IE I cannot make it base64 encoded, etc) and need to get a value to it with a new line in it.
I've tested it at http://htaccess.madewithlove.be/ and their tool thinks it should work!
EDIT:
We had a solution for the URL above but it didn't work with an example like:
/api/something/sent+40.06K+bytes++received+7.83M+bytes++542.70K+bytes%2Fsec%0Atotal+size+is+19.83G++speedup+is+2520.16+%28Completed+in+14+seconds%29.json
Thanks.
....and need to get a value to it with a newline in it.
You're missing the caret ^ considered the negation operator inside of your character class [^.]
RewriteRule ^api/([^.]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
See regex101 demo
I am working on a client's site that used to have all .swf files in directories via product code eg. g18/g18_flashfile.swf but now i've moved them into assets/flash/g18_flashfile.swf
I have tried to mod_rewrite the request to the new location due to external sites hotlinking to the file. This just error 500s
RewriteRule ^([^/]+)/([^.]+)\.swf$ assets/flash/$1/$2\.swf [L]
I also cannot just do a redirect anything as I am already using the following
RewriteRule ^(.*)/$ product.php?ref=$1 [L]
Any help would be great as I am scratching my head on this one.
EDIT
Whats even stranger is when I do
RewriteRule ^([^/]+)/([^.]+)\.swf$ assets/flash/$1/$2\.html [L]
It works (obviously it 404s because there isn't a .html file) but the rewrite works. Does anyone know if swf are some kind of term used in mod_rewrite?
The regular expression
^([^/]+)/([^.]+)\.swf$
matches both g18/g18_flashfile.swf and assets/flash/g18_flashfile.swf. Since the L flag might not work as you expected, this is a problem.
Just change the regular expression so that it doesn't match your rewritten path:
RewriteRule ^([^/]+)/([^/.]+)\.swf$ assets/flash/$1/$2\.swf [L]
Theese are my rules. First one is working but second one is not.
It was working on my old server. I Just changed my server now its not working.
RewriteRule ^oyunlar/([0-9]+)/([_0-9a-z-]+) games.php?id=$1&title=$2
RewriteRule ^post/([_0-9a-z-]+) post.php?up=$1
what could be wrong ?
on my php side, $_GET completely empty.
That is whole file;
RewriteEngine on
RewriteRule ^uye user.php
RewriteRule ^oyunlar/([0-9]+)/([_0-9a-z-]+) games.php?id=$1&title=$2
RewriteRule ^post/([_0-9a-z-]+) post.php?up=$1
RewriteRule ^tv tv.php
RewriteRule ^thumb timthumb2.php
ErrorDocument 404 /404.php
and that is the url i requested: myurl.com/post/login
i'm checking status with charles. ^post part is working but querysting part is not.
edit: i just made i a couple tests, and actually both rules not working properly and somehow, my files can work without extension like: /hello.php or /hello
additional info: this is my own private server, debian lenny + ispCP
#siniradam
Try changing your regex from this
([_0-9a-z-]+)
to this
([-_0-9a-z]+)
If a hyphen is supposed to be one of the allowed characters, it needs to be first in the list, not last.
Finally i've resolved my problem. it is occurs beacuse of MultiViews
somehow there was a rewrite and multiviews conflict. Simply i just disabled it from the .conf file.
Thank you people.