Apache rewrite condition get second regex match - regex

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]

Related

How to redirect from specific subdirectory to a subdomain via .htaccess?

I've been trying to redirect this URL (and all its substructures):
http://example.com/archive/
to (and its corresponding substructures):
http://archive.example.com/
For example: http://example.com/archive/signature/logo.png ==> http://archive.example.com/signature/logo.png
I tried to generate an .htaccess rule using a generator and evaluating it by looking at the regex, which I can understand (I think).
The result was the following rule:
RewriteEngine On
RewriteRule http://example.com/archive/(.*) http://archive.example.com/$1 [R=301,L]
The way I see it, the server will proccess any URL that starts with http://example.com/archive/ , will capture the string that comes next and will change the whole initial portion with the subdomain structure and append the captured string.
Unfortunately, this doesn't seem to work neither on my server, nor on online testing tools such as: http://htaccess.madewithlove.be/
Is there anything I'm missing there?
Thank you!
You should be able to try it this way.
RewriteEngine On
RewriteRule ^archive/(.*)$ http://archive.example.com/$1 [R=301,L]
Note that I did not make it dynamic as you didn't specific if you will have more URL's that need to work this way as well or not.

My apache rewrite only works for the first folder level

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.

Apache rewrite rule regex

I'm trying to write a rewrite rule to redirect files in my new webhelp system (it's a stand alone website).
I have two things I try to take into account -
I want to redirect only files that are in the http://www.mydomain.com/webhelp folder.
I need to change the .html in the end to .htm...
I manage to find each rule by itself by matching the http://www.mydomain.com/webhelp, but
when I try the rewrite cond it breaks...
Basically, its
http://www.mydomain.com/webhelp/hello.html
into
http://www.mydomain.com/webhelp/hello.htm
without changing
http://www.mydomain.com/index.html
Would really appreciate any help.
Thanks!
If you put the following in a .htaccess file in your webhelp folder, it should achieve what you're looking for:
RewriteEngine on
RewriteBase /webhelp/
RewriteRule ^(.*).html$ $1.htm [R=301,L]
You need to use backreferences. Something like:
RewriteRule ^www/webhelp/(.*)[.]html([#])(.*)[.]html$ www/webhelp/$1.htm$2$3.htm
RewriteRule ^www/webhelp/(.*)[.]html$ www/webhelp/$1.htm

Mod_rewrite swf file to another directory

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]

mod_rewrite problem with relative path css/js

Hi I have a problem.
I want to get all requests to redirect to index file in main directory and I've achieved this but there are problems with relative paths.
When I put address like: mydomain.com/something it works ok as the paths are relative to the main directory.
The problem is when I put something like: mydomain.com/something/somethingelse.
the .htaccess file:
Options FollowSymLinks
RewriteEngine On
# ignore anything that's an actual file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
# redirect all other traffic to the index page
RewriteRule . index.php [L]
Any ideas on how to get css/js working?
Edit:
The problem is that css/js files aren't loaded when the path entered have multiple slashes like:mydomain.com/something/somethingelse
It is no doubt better to use absolute path for static files (css, js, images etc). But if you lots of those instances in several pages then consider using HTML base tag to specify a default URL for relative paths. eg:
<base href="http://www.example.com/static/" />
Using the <base>-tag is a nice solution and most browsers seem to handle it well. Except there are some issues with IE, as was to be expected... Apparently you can also run into some other funny problems, see discussion here.
So for people where this is not an option, i have looked into the alternative (the "hard way").
Usually you store css/js/static images/other stuff like this:
index.php
js/
css/
imgs/
and you want the javascript and stylesheets etc. to be available, no matter how many slashes there are in the url. If your url is /site/action/user/new then your browser will request
/site/action/user/css/style.css
/site/action/user/css/framework/fonts/icons.ttf
/site/action/user/js/page.js
/site/action/user/js/jquery/jquery.min.js
/site/action/user/js/some/library/with/deep/dir/structure/file.map
So here are some rewrite rules for apache to solve this... First, if the target actually exists on disk, do not rewrite:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [L,QSA]
In words, IF reqest filename is a directory OR IF request filename is a file then do not rewrite (-), last rule (L) and pass any GET parameters (QSA, query string append). You can also use
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^.*$ - [L,QSA]
if you also need symlinks. Next we want the javascript and stylesheets to be found even if the requests assume a wrong base directory as shown above.
RewriteRule ^.*/js/(.*)$ js/$1 [L]
RewriteRule ^.*/css/(.*)$ css/$1 [L]
The pattern is pretty obvious, just replace 'css' with the directory name. There is still a problem with this, especially for large websites with lots of javascript and stylesheets, libraries etc. - The regex is greedy. For example, if you have a javascript directory like this:
js/some/library/js/script.js
and your request goes to /site/action/user/new, the browser will request /site/action/user/new/js/some/library/js/script.js, which the rewrite-engine will then rewrite to
js/script.js
because the first .* is greedy and matches /site/action/user/new/js/some/library. Switching to non-greedy regex does not really make sense, since "the rewrite engine repeats all the rules until the URI is the same before and after an iteration through the rules."
There is another problem, and that is that for every directory that needs to be exempted from rewriting, a relatively "expensive" regex is needed. Both problems can be fixed by just putting every static component into a subdirectory with an "unusual" name (and really this is the best solution imo - anyone with a better idea please post it).
The directory structure would then look like this:
index.php
mystrangedir/js/
mystrangedir/css/
mystrangedir/imgs/
Of course, this needs to be inserted everywhere in the code - for projects with a large existing codebase this can be tricky. However, you only need a single regex for directory exemption then:
RewriteRule ^.*/mystrangedir/(.*)$ mystrangedir/$1 [L]
Automated build systems (like gulp, grunt....) can be used to check if "mystrangedir" does not exist as directory anywhere below itself (which would again throw off the rewrite engine).
Feel free to rename mystrangedir to something more sensible like static_content but the more sensible it gets, the more probable it is that the directory name is already used in some library. If you want an absolutely safe directory name that has certainly never been used before, use a cryptographic hash, e.g. 010f8cea4cd34f820a9a01cb3446cb93637a54d840a4f3c106d1d41b030c7bcb. This is pretty long to match; you can make a tradeoff between uniqueness and regex performance by shorting it.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
Should obviously work despite the comments.
Try to add the RewriteLog and RewriteLogLevel directive to give us better details.
This is a path resolution issue: When using the relative path ./css on the base path /something it is resolved to /css while on /something/somethingelse it is resolved to /something/css.
This can’t (or rather shouldn’t) be fixed with mod_rewrite. Use absolute paths instead of relative paths, so /css instead of ./css.