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]
Related
I have a url like
domain.com/order/index.php?q=
the order folder no longer exists as its been changed to ecom, so it now looks like the below.
domain.com/ecom/index.php?q=
The problem is we have a ton of external links wanting the order url some scripts and images pull from this url, etc.
How can I simply set .htaccess to still allow the order directory.
I tried this
RewriteRule ^order/(.*)$ /ecom/$1 [L]
It works only if it exactly matches order (e.g domain.com/order/ , but anything after order it then breaks (e.g domain.com/order/index.php?q=)
Any help would be appreciated. I just keep seeing examples similar to what I have above so not sure if I am doing something wrong here.
Use QSA flag
RewriteRule ^order/(.*)$ /ecom/$1 [QSA,L]
you can do a permanent redirect to a page
RewriteRule ^url-string-to-redirect$ http://www.yourdomain.com/your-new-url-string [R=301,L]
I have been trying to work the regex out for this for a while now and I am struggling. Was hoping someone could help.
I have a website using apache mod_rewrite converting directories into get variables to 1 level. I am wanting to change this or add a seperate rule for the following
example.com/portfolio/plugins/jquery-tester
becoming
example.com/portfolio/handler.php?area=plugins&item=jquery-tester
I am trying to currently build up on php live regex but coming up trumps.
Try this:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/portfolio/(.+)/(.+)$ /portfolio/handler.php?area=$1&item=$2 [L]
Having the handler in the same directory as what you're matching can cause potential problems though (that's why the RewriteCond is there to check so you don't end up with an infinite loop).
If you have other rewrite rules, you may need to check that there aren't any conflicts.
RewriteRule ^/portfolio/[a-zA-Z\-_]+/[a-zA-Z\-_]+$ /portfolio/handler.php?area=$1&item=$2 [L]
I''m sorry for asking the millionth Apache rewrite question here. I tried everything I know, but there is a small (hopefully) step that I'm looking for someone to shed a light for me.
I have a URL structure similar to this:
- assets
- assets/dist/19854/css/my.css
- css/my.css
I'm trying to rewrite assets/dist/19854/css/my.css file to css/my.css file in the root.
I have mod_rewrite enabled on my server, and I have basic understanding of rewrite rules, but it would be great if you could assist me with the Regex.
RewriteCond %{REQUEST_URI} ^assets/dist/([0-9/.]+)/(.*)$
RewriteRule ^assets/dist/([0-9]+)/(.*)$ $ [L,QSA]
Problem with the above rule is the it rewrite to ./19854, but I'm actually interested in the second expression's match. If possible, I'd also like to make sure the css/my.css file exists first.
Thanks in advance!
Edit:
Thanks for the comments and the answer. To further explain my case, this is a small site that uses a CDN, and everytime a new build is up, the number in assets/dist/[0-9] gets changed, so all assets' source URL gets changed. But I'm using a CSS compiler to compile CSS files, so CSS files reside in the same folder (css/my.css).
I have some other rewrites so I'm making my RewriteConds more strict.
So far, the above rewrite matches the numeric part, but I'm trying to rewrite to the URL right after the numeric part.
I believe you're attempting in other way round. You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# route to /css/my.css if it exists
RewriteCond %{DOCUMENT_ROOT}/$1 -f [NC]
RewriteRule ^assets/dist/.+?/(css/my\.css)$ $1 [L,NC]
We have a website where we show clients creative work we have produced for them. We upload raw assets to a path like this:
x.com/clients/clientName/campaignName/size/
I have a PHP script which adds our branding, contact information and other information and pulls in the raw creative (usually a swf object). It is in this directory x.com/clients/index.php and it accepts a query string parameter ?path so it knows where to look for the creative.
I am trying to do an apache rewrite in .htaccess so that our designers can upload directly to the known folder structure but so that when you go to x.com/clients/clientName/campaignName/size/ it should rewrite to x.com/clients/index.php?path=clientName/campaignName/size/
I am currently using the following rewrite rule, which works for the first folder level e.g. x.com/clients/clientName/ does successfully rewrite, but any subsequent folders do not.
RewriteRule ^clients/([^/\.]+)/?$ /clients/index.php?path=$1 [L]
My RegEx's are terrible, so I'm stuck on what to do. Any help appreciated, thank you kindly.
Your regex is only matching urls like clients/xxxxxx/ because your pattern [^/\.]+ means one or many characters except "/" or "."
With your rule, it can't work for other subdirectories.
You can change your rule by this one
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
To avoid internal server error (code 500 which means an infinite loop in this case), you can do it this way
RewriteRule ^clients/index\.php$ - [L]
RewriteRule ^clients/(.+)$ /clients/index.php?path=$1 [L]
Is there a special reason you want to use regex? In my opinion you can just catch everything coming after /clients:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]
RewriteRule ^clients/(.*)$ /clients/index.php?path=$1 [L]
The second line is to prevents redirect loops, because the index.php is also in the folder /clients and this would cause never ending redirects.
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.