HTACCESS With exception for certain folders - regex

I have an old website i'm trying to take over, and it has a bad htaccess file here that I need to adjust to enable 'FOLDERS', instead of routing EVERYTHING.
So I have a folder called, "RESOURCES", but it's trying to route through the index file with it..... but I need to load the resource folder instead of routing. How do I make an exception?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.website\.ca$ [NC]
RewriteRule .? http://www.website.ca%{REQUEST_URI} [R=301,L,QSA]
RewriteRule ^(browse)/?$ /browse.php [L,QSA]
RewriteRule ^([\ \+\-0-9a-zA-Z_]+)/?$ /index.php?where=$1 [L,QSA]
#RewriteRule ^([\ \+\-0-9a-zA-Z_]+)/([-0-9a-zA-Z_]+)/([-0-9a-zA-Z_]+)/?$ /index.php?where=$1&what=$2 [L,QSA]
RewriteRule ^(-0-9a-zA-Z_]+)/([-0-9a-zA-Z_]+)/?$ /index.php?where=$1&what=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [R=301,L,QSA]
</IfModule>

Add this rule just below RewriteBase /
RewriteRule ^RESOURCES/- [L,NC]
This will skip rest of the rewrite rules for folder called RESOURCES
To skip all the folders use this rule:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^- [L]

Related

Apache .htaccess file not rewriting properly

I have an .htaccess file. It needs to rewrite URLs in two cases: for the MVC framework and in a more special case. I want to rewrite any requests to "/resources/newspaper" if the user doesn't have a cookie named "cfnw-hash". I've tried putting the code before the MVC code and after it. Doesn't work. It worked before switching over to the MVC framework, though I really don't have enough .htaccess knowledge to see if that's causing the issue. Here's code:
Options -Indexes
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
RewriteEngine On
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule \/resources\/newspaper.* http://www.example.com/error/401 [R=401,NC,L]
Combine your rules and I think you RewriteRule might not be matching because of the prepending / and also the cookie matching. Try this. Notice the cookie part that you update with your cookie.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cookie_name=specific_value; [NC]
RewriteRule ^resources/newspaper/?(.*) http://www.example.com/error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Have it this way:
<IfModule mod_rewrite.c>
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !cfnw-hash [NC]
RewriteRule ^resources/newspaper /error/401 [R=401,NC,L]
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>
Test this after clearing your browser cache.

htaccess redirect from a friendly url to one with parameters

I'm trying to create a rewrite rule that redirects vistors from one url to another on an Ecwid store, but I can't seem to get the syntax correct.
I'd like to redirect visitors from :
http://domain.ca/category/#######
to
http://domain.ca/products/#!/~/category/id=#######&offset=0&sort=normal
I've tried several variations below, but all generate a 500 Internal Server Error
Rewrite Rule ^/category/(.*)$ http://domain.ca/products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L]
Rewrite Rule ^/category/(.*)$ http://domain.ca/products/#\!/~/category/id\=$1&offset\=0&sort\=normal [R=301,L]
Rewrite Rule ^/category/(.*)$ /products/#\!/~/category/id\=$1&offset\=0&sort\=normal [R=301,L]
Rewrite Rule ^/category/(.+)$ http://domain.ca/products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L]
Here are the existing rewrite's in the htaccess file, using anubhava's example below
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^category/(.+)$ /products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L,NE]
</IfModule>
You have syntax issues. Use this rule:
RewriteEngine On
RewriteBase /
RewriteRule ^category/(.+)$ /products/#!/~/category/id=$1&offset=0&sort=normal [R=301,L,NE]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

multiple .htaccess files conflicting with each other

I have a shared hosting with multiple domain hosted on it. In the root folder of my hosting exists a .htaccess (say is htaccess1). Code in ht1 is:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$ [NC]
RewriteRule ^(/)?$ mydomain/index.php [L]
These rules are there so that only urls with www.mydomain.com use all files of mydomain folder. I am new to url rewriting so I don't understand the meaning of what each line does. Another .htaccess (say htaccess2) file exists in mydomain folder. Code is htaccess2 is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)?/$ try.php?id=$1
RewriteRule ^([0-9]+)?$ try.php?id=$1
RewriteRule ^post/([0-9]+)?/$ post.php?id=$1
RewriteRule ^post/([0-9]+)?$ post.php?id=$1
In this file, line 6,7 works fine and redirect requests with numeric parameters to try.php but lines 8,9 doesn't work and gives a 404 page not found error, exact error string is :
The requested URL /mydomain/post/1233445 was not found on this server.
I doubt that some effect of htaccess1 is creating this problem as I tried many variations of regex in line 8,9. Please help.
Main .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/mydomain/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mydomain/$1 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^$ /mydomain/index.php [L]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Added required options.
mydomain .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^post/([0-9]+)/?$ post.php?id=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)/?$ try.php?id=$1 [L,QSA]
Changes:
Added RewriteBase at the top.
Added L flag to mark it last rule.
Removed a redundant rule.
Moved specific rule above the generic rule.
Added required options.

opencart - multistore and rewritebase

I have in my root web folder public_html/ installed my info webpage using rewrite rules (seo). In subfolder public_html/shop/ I have installed Opencart so the access to cart is http://www.mydomain.com/shop/. Each folder public_html, public_html_shop/ has own .htaccess file.
I would like to use OC1551 as multistore on several domains redirected to this folder, but also as www.mydomaincom/shop/ - not redirected: www.mydomain1.com, www.mydomain2.com and also as www.mydomain.com/shop/
*public_html/shop/.htaccess* file need to set rewritebase as /shop/ for properly working with info webpage, because use rewriting rules for www.mydomain.com/, but for www.mydoaminX.com need to be set as rewritebase / .
Can you pls someone help me with setting rewritebase in these folders to work it right? Something like dynamicaly changing rewritebase in public_html/shop/.htaccess file.
Thanks
file public_html/.htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain.sk/$0 [NC,L,R=301]
RewriteRule ^(.*)/(.*)-([1-9])/(.*)/$ /index.php?category=$1&page=$2&gallery_page=$3&photo=$4 [L,QSA]
RewriteRule ^(.*)/(.*)/(.*)/$ /index.php?category=$1&page=$2&photo=$3 [L,QSA]
RewriteRule ^(.*)/(.*)-([1-9])/$ /index.php?category=$1&page=$2&gallery_page=$3 [L,QSA]
RewriteRule ^(.*)/(.*)/$ /index.php?category=$1&page=$2 [L,QSA]
RewriteRule ^(.*)/$ /index.php?category=$1 [L,QSA]
Rewriterule ^subscribe.php(.*)$ /index.php?category=overenie&addr=$1 [L,QSA]
Rewriterule ^sitemap.xml$ /webmap.php?engine=google [L,QSA]
Rewriterule ^urllist.txt$ /webmap.php?engine=yahoo [L,QSA]
file public_html/shop/.htaccess
RewriteEngine On
RewriteBase /shop/
RewriteCond %{HTTP_HOST} ^maydomain1\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain1.com/$0 [NC,L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain2\.com$ [NC]
RewriteRule ^.*$ http://www.mydomain2.com/$0 [NC,L,R=301]
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I actually find on the majority of servers that you can just remove the RewriteBase completely and it's smart enough to work out what it is. I would recommend trying this and seeing if it works

rewrite rule for WordPress 3.3 permalinks is not working

Every since an upgrade to WordPress 3.3 URLs are not redirecting as they should.
Changed: domain.com/2010/10/postname/ to: domain.com/postname/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
The problem was due to the leading slash and not using $3
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{1,2})/(.+)$ /$3 [NC,R=301,L]
There's a script here you can use to generate .htaccess rules if you want to change permalinks to the /%postname%/ structure.
http://yoast.com/change-wordpress-permalink-structure/
My permalinks were exactly the same as yours, I used this tool to change them and it is working well.
The last rule will never get applied if the previous rule matches. Assuming that the http://domain.com/2010/10/postname/ request doesn't match a file or directory, the RewriteRule . /index.php [L] is going to rewrite the URI to /index.php thus it'll never get to your rule. Try moving your rule up to the top, just below RewriteBase /, and duplicate the !-f/!-d conditions, so that it looks like this:
RewriteBase /
# for 301 redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/[0-9]{4}/[0-9]{2}/(.+)$ /$1 [NC,R=301,L]
# the rest of the rules
RewriteRule ^atom.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss.xml$ feed/ [NC,R=301,L]
RewriteRule ^rss2.xml$ feed/ [NC,R=301,L]
RewriteCond %{HTTP_USER_AGENT} !FeedBurner [NC]
RewriteCond %{HTTP_USER_AGENT} !FeedValidator [NC]
RewriteRule ^feed/?([_0-9a-z-]+)?/?$ http://feeds.feedburner.com/handle [R=302,NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also, if this is in an .htaccess file, you need to remove the leading slash in the rule match so that it looks like this: ^[0-9]{4}/[0-9]{2}/(.+)$