htaccess disallow root directory but allow other urls - regex

I'm using Xfilesharing for our company and I need to block the access to the main upload page by our internal IP range 192.168.230.1-225 and allow the download to every IP.
Using the following rule will block the main page but also the rest. But using a rule to allow the download breaks the rewrite rules.
RewriteEngine on
RewriteRule ^$ /cgi-bin/index.cgi [L]
RewriteCond %{REMOTE_ADDR} ^192\.168\.230\.25[0-5]|2[0-4]\d|1?\d\d?$
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L,QSA]
RewriteRule ^.*$ /error [R=302,L]
RewriteRule ^del\-([0-9A-Z]{12})\-([0-9A-Z]+)/.+$ /cgi-bin/index.cgi?del=$1-$2 [L]
RewriteRule ^admin$ /?act=admin [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z0-9\-\_]+).html$ /cgi-bin/index.cgi?act=page&tmpl=$1 [L]
The literal URL is http://www.example.com/MG4YBAOW18QH/3.png and the rule: ^([0-9A-Z]{12})/([^\/]+)$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L]

Have your download rules like this:
RewriteCond %{REMOTE_ADDR} ^192\.168\.230\.\d+
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ - [F,NC]
RewriteRule ^([0-9A-Z]{12})/([^/]+)/?$ /cgi-bin/index.cgi?act=download1&id=$1&fname=$2 [L,QSA,NC]

Related

Use .htaccess to rewrite a sub directory to root but keep access to other sub directories

I am trying to rewrite a subdirectory out of my URL structure but have other URLs within that directory structure also work. I have this scenario:
// This should load the content of mysite.com/foo/15 but not redirect
mysite.com/foo/
// Subdirectories within /15 should also work
mysite.com/foo/bar // the actual location of this is mysite.com/foo/15/bar
// Other URLs within this directory structure should also work, such as
mysite.com/foo/12
mysite.com/foo/13
mysite.com/foo/14
// Lastly, we have redirects from 2012 -> 12, 2013 -> 13 such that
mysite.com/foo/2012 becomes mysite.com/foo/12
mysite.com/foo/2013 becomes mysite.com/foo/13
... and so on
Here's where i'm at. I can't get the right combination of htaccess rules to achieve what I want.
RewriteEngine On
RewriteBase /foo/
// This makes mysite.com/foo/ load the content of mysite.com/foo/15 without redirecting, but makes mysite.com/foo/14 404
RewriteRule ^((?!15/).*)$ 15/$1 [NC,L]
// This works pretty well, except the rules below don't redirect
RewriteRule ^$ 15/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 15/$1
// Rewrite old years to the two digit form
RewriteRule ^2010/(.*)$ 10/$1 [R=301]
RewriteRule ^2011/(.*)$ 11/$1 [R=301]
RewriteRule ^2012/(.*)$ 12/$1 [R=301]
RewriteRule ^2013/(.*)$ 13/$1 [R=301]
RewriteRule ^2014/(.*)$ 14/$1 [R=301]
RewriteRule ^2015/(.*)$ 15/$1 [R=301]
What am I missing? What combination of rules will help me achieve what i'm looking for?
Your redirect rules can be combined into single rule using regex matching:
RewriteEngine On
RewriteBase /foo/
// Rewrite old years to the two digit form
RewriteRule ^20(1[0-5])/(.*)$ $1/$2 [R=301,L]
RewriteRule ^$ 15/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!15/).*)$ 15/$1 [L]
Try including the L flag in all your rules and then put all your redirects at the top of the htaccess file:
RewriteEngine On
RewriteBase /foo/
// Rewrite old years to the two digit form
RewriteRule ^2010/(.*)$ 10/$1 [R=301,L]
RewriteRule ^2011/(.*)$ 11/$1 [R=301,L]
RewriteRule ^2012/(.*)$ 12/$1 [R=301,L]
RewriteRule ^2013/(.*)$ 13/$1 [R=301,L]
RewriteRule ^2014/(.*)$ 14/$1 [R=301,L]
RewriteRule ^2015/(.*)$ 15/$1 [R=301,L]
// This makes mysite.com/foo/ load the content of mysite.com/foo/15 without redirecting, but makes mysite.com/foo/14 404
RewriteRule ^((?!15/).*)$ 15/$1 [NC,L]
// This works pretty well, except the rules below don't redirect
RewriteRule ^$ 15/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 15/$1 [L]

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]

HTACCESS With exception for certain folders

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]

.htaccess not working only with internet explorer

i have site, that has to rewrite site.ru and www.site.ru to www,site.ru/ru_RU.
I can't access any Apache config files. In htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{HTTP_HOST} ^www.site.ru$
RewriteRule (.*) http://www.site.ru/ru_RU [QSA]
RewriteCond %{REQUEST_URI} ^/news
RewriteRule (.*) /news [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>
It's working in firefox or chrome, but in IE i get "this page can't be displayed". Tested on IE10 and IE8 (not compatibility view) on few computers.
If i write some junk in .htacess, i get 500 error in IE. Without .htaccess site loads ok, but i need it to rewrite url. Any ideas how to fix it?
Your flags are all wrong. Modify your rules to this:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site\.ru$ [NC]
RewriteRule ^ http://www.site.ru/ru_RU [L,R]
RewriteCond %{HTTP_HOST} ^www\.site\.ru$ [NC]
RewriteRule !ru_RU /ru_RU [NC,L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L,QSA]
</IfModule>
Reference: Apache mod_rewrite Introduction
I can't test it now, but here is my guess:
I think http_host ONLY contains the host part, not the uri. So you end up with an endless loop as www.site.ru is will always match again after your rewrite.
You will need another rule that checks if the uri is empty. Like this (untested):
RewriteCond %{REQUEST_URI} ^$
RewriteRule (.*) /ru_RU [QSA]
You might need another condition for the case that the uri contains a slash.

Remove 'index.php' from URL with .htaccess

I've been trying all sorts of solutions from this site and none seem to work. I'm currently hosting with hostgator. This is my current .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
<IfModule mod_suphp.c>
suPHP_ConfigPath /home/user/php.ini
<Files php.ini>
order allow,deny
deny from all
</Files>
</IfModule>
This is in the root folder of my site. I have also tried adding a ? after index.php and no luck. Does anyone know why this isn't working?
This is the code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
Symfony 2 has an excellent solution:
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by apache
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/index.php [L]
This accomplishes the following:
RewriteBase is not necessary (useful when the site is in a subdirectory beneath the web root)
index.php is removed if present
The request is routed to the correct index.php with the full query string from the original request
Note that the line:
RewriteRule ^(.*) - [E=BASE:%1]
is responsible for setting the %{ENV:BASE} variable for later on. Refer to Apache documentation on E|env flag.
I tried this and works fine:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
Exceptions
If your site’s system directory (/system/) has been renamed and is still accessible by URL, modify the RewriteCond line above:
RewriteCond %{REQUEST_URI} !/newdirectoryname/.* [NC]
If you are running EE from a sub-directory rather from the root of your domain (e.g. http://example.com/myeesite/ instead of http://example.com/), just remove the slash preceding index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ index.php/$1 [L]
If you are running EE from a sub-directory and it still doesn’t work after removing the slash, you may need to specify the sub-directory in your rewrite rule. For example, if your sub-folder is named testing, change:
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
To:
RewriteRule (.*?)index\.php/*(.*) testing/$1$2 [R=301,NE,L]
And change:
RewriteRule ^(.*)$ /index.php/$1 [L]
To:
RewriteRule ^(.*)$ testing/index.php/$1 [L]
If your host requires forcing query strings, try adding a question mark following index.php in the RewriteRule line above, like so:
RewriteRule ^(.*)$ /index.php?/$1 [L]
To remove index.php from urls on apache2.4 you can use the following rule :
RewriteEngine on
RewriteRule ^index\.php/(.+)$ /$1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !index\.php /index.php%{REQUEST_URI} [L,END]
This will change the uri
/index.php/foobar
to
/foobar
This rule will return an internal server error for lower versions of apache as they don't support the END flag. Please see the anubhava's answer above that works almost on all versions.
I've been compelled to join stack overflow.com today to comment here as the answer has solved a long term problem I've had with a Zend Framework website. I've worked on the website around 3 1/2 years and during that time I discovered that it didn't handle index.php correctly and this causes webmaster tools to see duplicate titles etc.
I decided to search again today for a solution, one of many times attempted.
This is the one (the last answer shown above) that solves my problem.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
As an example. this is what it achieves for me.
http://www.domainname.co.uk/index.php/categories/url-name.html
becomes
http://www.domainname.co.uk/categories/url-name.html
Thank you to everyone who contributed to the original question as it lead to the answer and solution above.
Extra Note: I have other rewrite commands that handles the other aspects but those on their own didn't fix the index.php and the only time this has been fixed is by using.
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
I only hope it helps others whether its ExpressionEngine or Zend Framework, in the future.