HTACCESS Rewrite: Ignore another rewrite rule - regex

I am using rewrite rules for two purposes. First, I am able to rewrite the URL's to my .css and .js files so they can be processed through a minimize script. This has been working fine:
RewriteEngine on
RewriteRule ^assets/css/min/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)\.css$ assets/css/min.php?style=$1&theme=$2&ver=$3 [L,QSA]
RewriteRule ^assets/js/min/([a-zA-Z0-9\._-]+)/([a-zA-Z0-9\._-]+)\.js$ assets/js/min.php?scripts=$1&ver=$2 [L,QSA]
However, now I added a rule to redirect all traffic for nonexistent pages to my index.php file (for MVC routing):
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteBase /
The problem is, now that I have both of these rules in my HTACCESS file, I am no longer able to access my .css or .js files like:
http://domain.tld/assets/css/min/all/default/0.2.3.css
How can I get the rerouting to index.php to ignore the previous rule with my css and js rewriting?

You can use a negative lookahead in last rule to skip assets/ directory:
RewriteEngine on
RewriteRule ^assets/css/min/([\w.-]+)/([\w.-]+)/([\w.-]+)\.css$ assets/css/min.php?style=$1&theme=$2&ver=$3 [L,QSA]
RewriteRule ^assets/js/min/([\w.-]+)/([\w.-]+)\.js$ assets/js/min.php?scripts=$1&ver=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!assets/)(.+)$ index.php/$1 [L,NC]
Also note that you can replace [a-zA-Z0-9_] with shorter \w

Related

RewriteRule if first character is question mark

I have a problem with my .htaccess file.
At the moment I use the following script for every request under the subdir /lp:
RewriteEngine On
RewriteBase /lp
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png)$ [NC]
RewriteRule . index.php
If someone uses an URL like the following
www.website.com/?1FOOBAR23
I would also like to redirect to the subdirectory /lp with the same rule as above. But only if the URL starts with a question mark followed by a number and 6-8 other chars. How could I do that?
You will need this rule in site root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]{6,}$ [NC]
RewriteRule ^/?$ ip/index.php [L]

Htaccess Public Folder Rewrite Rule

I'm working on a little custom MVC project in PHP and am having some issues with the htaccess. Originally, my htaccess was routing all traffic to index.php in my root dir, and passing the remaining path as an argument. This was working perfectly with this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?path=$1 [NC,L,QSA]
The problem I'm having now is that I need to move the index.php file into a /public directory. I scoured the internet for answers, and found a code snippet that kinda works in that it seems to get there as long as it is just hitting /, but as soon as the url becomes /register/ or anything else it just 404's.
# Get rid of /public/ in the URL, and route all requests through
# the Index.php file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /public/index.php?path=$1 [NC,L,QSA]
RewriteCond %{REQUEST_URI} !/public/ [NC]
RewriteRule ^(.*?)/?$ public/$1 [L]
I know that last line makes no sense in when there is the rewrite to index.php with ?path that seems proper to me (at least it passes the argument like I want!) but without both these lines it doesn't seem to work, and I've been trial-and-erroring this for hours. Hopefully someone can help out! Cheers!
Keep only this content in your .htaccess:
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^/?$ public/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/?path=$1 [L,QSA]

htaccess - Rewriterule redirects to wrong URL

I have a simple rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA]
Anything that is not an existing file should be forwarded to index.php. However, there's a directory, images, there are sevenal images in it, but no index.php|html. When I open localhost/images, without a closeing slash (localhost/images/ works fine), it redirects me to localhost/images/?p=images. How should I solve that?
That is happening because of the mod_dir module that runs after mod_rewrite adding a trailing slash to directories. You should add a condition to avoid rewriting directories in your rule. Also add a trailing slash using a redirect rule:
RewriteEngine on
# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?p=$1 [QSA,L]

Rewrite urls to not include file extensions but allow subdomains to act normally

I need to
Remove the file extension from all of the urls on my site (mywebsite.com/about.html >> mywebsite.com/about/)
Always add a trailing / to the end of the url (mywebsite.com/about >> mywebsite.com/about/)
Allow for an exception that one of the nav items links to a pdf, not an html document (mywebsite.com/calendar.pdf >> mywebsite.com/calendar/
Allow subdomains to go to their folder instead of being rewritten (My folder tree is public_html>my main site files and a dev folder>(inside dev folder) index.html) I need the subdomain to link to that dev folder, currently the url rewriting changes dev.mywebsite.com to public_html/dev.html
This is my current, hacked together htaccess file:
RewriteEngine On
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Sorry, this might be a duplicate or semi-duplicate. I looked around as much as I could but couldn't find anything that applied to my situation exactly or that I understood. Thanks guys!
Try this code:
RewriteEngine On
RewriteBase /
# skip dev. subdomain
RewriteCond %{HTTP_HOST} ^dev\. [NC]
RewriteRule ^ - [L]
# calendar rule
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf [L]
# hide .html rules
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1/ [R=301,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Apply rewrite rule for all but all the files (recursive) in a subdirectory?

I have an .htaccess file in the root of the website that looks like this
RewriteRule ^some-blog-post-title/ http://website/read/flowers/a-new-title-for-this-post/ [R=301,L]
RewriteRule ^some-blog-post-title2/ http://website/read/flowers/a-new-title-for-this-post2/ [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
## Redirects for all pages except for files in wp-content to website/read
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/wp-content
RewriteRule ^(.*)$ http://website/read/$1 [L,QSA]
#RewriteRule ^http://website/read [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My intent is to redirect people to the new blog post location if
they propose one of those special blog posts.
If that's not the case
then they should be redirected to http://website.com/read.
Nothing from http://website.com/wp-content/* should be redirected.
So far conditions 1 and 3 are being met. How can I meet condition 2?
Note: I want to redirect to /read even for content that exists to prevent any images, CSS, or javascript from working, except those in /wp-content
Make your first rule as:
## Redirects for all pages except for files in wp-content to website/read
RewriteRule !^wp-content/ http://website.com/read%{REQUEST_URI} [NC,L,R=302]