Identical rewrite rules for multiple websites in apache.conf - mod-rewrite

I'm trying to setup multiple websites in Apache.conf to include identical RewriteRules, and avoid having to use .htaccess files in the root dir of each website. The websites are all running the same application, and the .htaccess files are identical. This post gets a bit long so please bear with me.
Applying the rules separately to each directory basically WORKS:
<Directory /var/www/sites/sitename/>
# Rewrite any calls to *.html, *.json, *.xml, *.atom, *.rss, *.rdf or *.txt if a folder matching * exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !public/
RewriteCond %{DOCUMENT_ROOT}/public/$1.$2 !-f
RewriteRule (.+)\.(html|json|xml|atom|rss)$ $1/ [L]
# Add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.|\?)
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ([^/]+)$ $1/ [L]
# Rewrite any calls to /render to the image parser
RewriteCond %{REQUEST_URI} render/
RewriteRule ^render/. app/parsers/slir/ [L]
# Rewrite routes to index.php if they are non-existent files/dirs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ index.php?/$1/ [L,QSA]
# Rewrite any file calls to the public directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !public/
RewriteRule ^(.+)$ public/$1 [L]
# Allow basic authentication
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</Directory>
I have 20+ websites that require the same rules, so it seems pretty ineffective having to include the entire block above for each directory. I have tried a few things, and although I feel I'm onto something, they produce wrong results:
<DirectoryMatch /var/www/sites/[^/]+/>
{{ RULES_BE_HERE }}
</DirectoryMatch>
<Directory /var/www/sites/.*/>
RewriteBase /
{{ RULES_BE_HERE }}
</DirectoryMatch>
The best solution I can come up with, is using an Apache include file for each directory like this:
<Directory /var/www/sites/site1/>
Include /etc/apache2/myrules.conf
</Directory>
<Directory /var/www/sites/site2/>
Include /etc/apache2/myrules.conf
</Directory>
<Directory /var/www/sites/site3/>
Include /etc/apache2/myrules.conf
</Directory>
Obviously I don't have too much experience with apache.conf and rewrite rules. Anyone has any sound advice? Any help appreciated.

Related

how to allow /.well-known/security.txt in htaccess

I want to whitelist this file only in .well-known directory .
My htaccess file
Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
<IfModule mod_rewrite.c>
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule "(^|/)\." - [F]
</IfModule>
my attempts
<Files .well-known/security.txt>
Allow from all
Satisfy Any
</Files>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^(public|.well-known/security.txt)
RewriteRule ^(.*)$ public/$1 [L]
Conclusion !
I just want to whitelist security.txt file only in .well-known directory
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
This condition is in error since the REQUEST_URI server variable always starts with a slash, so the expression !^public is always successful.
(From this I assume you must have another .htaccess file in the /public subdirectory, otherwise you would get a rewrite loop.)
Once you realise this then you can modify the rule in the way you had tried. For example:
RewriteCond %{REQUEST_URI} !^/(public|\.well-known/security\.txt$)
RewriteRule ^(.*)$ public/$1 [L]
HOWEVER, your other rule blocks all requests for physical files/directories that start with a dot, so the request will still be blocked (with a 403 Forbidden). This rule is also in the wrong place for other requests (it needs to be before the rewrite to the /public subdirectory).
You are better off making an exception as a separate rule at the start of the file instead. For example, try the following instead:
Options -Indexes
RewriteEngine on
# Prevent further processing for special files
RewriteRule ^\.well-known/security\.txt$ - [L]
# Block access to all dot-files
RewriteCond %{SCRIPT_FILENAME} -d [OR]
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule (^|/)\. - [F]
# Rewrite request to "/public" subdirectory
RewriteCond %{REQUEST_URI} !^/public
RewriteRule (.*) public/$1 [L]
The <IfModule> wrapper on that one rule was superfluous.

Why can I not remove index.php from codeigniter URL

I have a new installation of Kubuntu 15.10 with old (working) code from my site.
Somehow I can not get rid of the index.php in the URL.
I tired dozens of suggestions but nothing seems to work.
My current status is this:
Apache 2 have mod_rewrite on:
printout of php_info()
Apache2 config is:
<Directory /var/www>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
My .htaccess is located under application folder:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
What am I missing here? I am loosing faith... :-P
Step 1: In your .htaccess replace all other codes..try with this:
RewriteEngine On
RewriteRule ^(application) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
Step 2: Move your .htaccess from application folder to where index.php located.
Step 3: Change in your config.php
From:
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';
Try it in application/.htaccess, index.php will exists in url but in scripts it will be ignored.
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]

In codeigniter redirecting is not working without index.php?

I am new to codeigniter while redirecting a page index.php is not loading by default,So i have tried the below code in htaccess file. I have installed php 5.3.10, apache server and postgres database. so if any new configuration have to be done apart from changing .htaccess and httpd.conf then please let me know. I have seen previous questions asked on the same issue and i referred everything from those questions but still I am not able to solve this. please let me know what extra things to be done.
.htaccess file
<IfModule mod_rewrite.c> RewriteEngine On #Checks to see if the user is attempting to access a valid file, #such as an image or css document, if this isn't true it sends the #request to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d #This last condition enables access to the images and css folders, and the robots.txt file RewriteCond $1 !^(index\.php|public|images|robots\.txt|css) RewriteRule ^(.*)$ index.php/$1 [L] </IfModule>
RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond $1 !^/(index\.php|assets/|humans\.txt) RewriteRule ^(.*)$ index.php/$1 [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
httpd.conf file
ServerName localhost
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Following things you should do for avoiding index.php in url
Always make sure you have .htaccess file. which you have already.
Include contents in .htaccess which #charlie has suggested above.
Edit httpd.conf file using nano command and include main thing keep allowoveride All
First disable rewrite mode and enable it again then restart apache2 server using following commands.
sudo a2dismod rewrite //which disables the rewrite mode
sudo a2enmod rewrite //which enables the rewrite mode
sudo service apache2 restart //which restarts your apache
Thats all you need to do. All the best.
Paste this in your .htaccess file
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

Skipping a entire directory using Helicon ISAPI rewrite v3.0

Here is my current .htaccess file. This file is in my root directory for my site.
I want to add a rule that in plain english will do the following.
COMPLETELY SHUT OFF RULES PROCESSING FOR
http://www.sc-pa.com/dr405/*.*
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (www.sarasotaproperty.net|www.sc-pa.net|sc-pa.net|sarasotaproperty.net) [nc]
#RewriteRule ^(.*)$ http://www.sc-pa.com/$1 [R=301,NC,QSA]
RewriteRule (.*)/eurl\.axd/[0-9a-f]+ /$1 [L]
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteCond %{REQUEST_URI} !.*(js|css|inc|jpg|gif|png)
RewriteRule (.*) ${lc:$1} [R=301]
RewriteCond %{REQUEST_URI} !.*(web_content/pdf/|/dr405/).*
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (?!.*/web_content/pdf/)([^/]*?\.pdf) /web_content/pdf/$1 [R=301]
RewriteRule pasite-(.*\.asp)$ /content/$1 [R=301,QSA]
RewriteRule home\.asp$ / [R=301]
#RewriteRule ^search/tpp/?$ content/search_tangible.asp
#RewriteRule ^search/?$ content/search_real_property.asp
#RewriteRule ^downloads/?$ content/downloads.asp
#RewriteRule ^(.*?view)/([^/]*)/([^/]*)(/.+)? /search/parcel_detail.asp?account=0&$2=$3 [NC,LP,QSA,R=301]
If you need to completely disable rewriting and .htaccess files for this directory and subdirectories you may use section in the HTTPD.CONF file. Here is an example:
<Directory C:/inetpub/mysite/dr405>
RewriteEngine off
AllowOverride None
</Directory>
Please note you need to use fully qualified path, i.e. C:/intepub...
If you simply want to exclude this directory so rules from current .htaccess file will not be applied you may place this rule at the beginning of your site's .htaccess file:
RewriteBase /
RewriteRule ^dr405 - [L]
RewriteCond %{HTTP_HOST} (www.sarasotaproperty.net|www.sc-pa.net|sc-pa.net|sarasotaproperty.net) [nc]
#RewriteRule ^(.*)$ http://www.sc-pa.com/$1 [R=301,NC,QSA]
RewriteRule (.*)/eurl\.axd/[0-9a-f]+ /$1 [L]
# ... etc....
Moved the site to IIS7 and used the built in rewrite module.

having issue site made in codeigniter is shifted to new domain

hey i have a running site which URL is below
http://www.buraksolutions.com/homelife/site/residential.html
the problem is when i shifted this site to other domain, there is index.php? coming in the URL which is causing a lot of problem, it does not loads most of data
http://nextcrawl.net/HomeLife/site/index.php?/welcome_controller.html
here is my .htaccess file
RewriteEngine on
#RewriteBase /kunden/homepages/8/d232327588/htdocs/consulnet/genetech/mockups/nextcrawl/homelife/site
#RewriteCond $1 !^(index\.php|resources|robots\.txt)
#RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^(.*)$ /index.php/$1 [L]
# AddType x-mapp-php5 .php
#Options +FollowSymLinks
RewriteEngine On
RewriteBase /homelife/site
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php?/$1 [L]
Take a look at the URL rewriting that's defined in your .htaccess file. It's possible that its configuration doesn't match with the new domain name, or mod_rewrite may not even be enabled on your new domain's server. I can't tell you more than that without looking at your setup, but here are some fantastic resources on URL rewriting with CodeIgniter:
http://codeigniter.com/user_guide/general/urls.html
http://codeigniter.com/wiki/mod_rewrite

Resources