How to put my .htaccess in my virtualhost? - apache

I would like to know if it's possible to put .htaccess instructions directly in my virtualhost.
My .htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I try on my virtualhost (some instructions are deliberately deleted for this topic) :
<VirtualHost *:80>
ServerName domain.dev
ServerAlias www.domain.dev
<Directory /www/htdocs/domain/html/>
Options -Indexes FollowSymLinks MultiViews
AllowOverride All
</Directory>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
</VirtualHost>
But my browser responds :
Bad Request
Your browser sent a request that this server could not understand.

It's work with :
<Directory /www/htdocs/domain/html/>
Options -Indexes FollowSymLinks
AllowOverride All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</Directory>

Related

how set mod_rewrite by ip all pages

i use friendly url names like myservice/controller/action/key/value/key/value etc. Everything is ok when i am running my server on remote hosting. But now i tried run server on my linux and i have a access by ip. My mod_rewrite rules doesn't work. Could someone tell me how can i change this ?
my old rules working on remote hosting with domain
rewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options +FollowSymLinks -Indexes -MultiViews
And this is my new rules not working
rewriteEngine On
RewriteCond %{REMOTE_ADDR} !^192\.168\.1\.104$
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Ok i tried many times and this rules works fine
rewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Options +FollowSymLinks -Indexes -MultiViews
But in vhost in apache2/available-sites i had to add
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require all granted
</Directory>
Thanx for help

rewritecond not working for existing files

I have a problem with mod rewrite apache seems to ignore the rewrite cond.
The rewrite rule is working as all my pages are working but the problem is with resources like css, imgs and js. In my html I use "href=assets/css/style.css" which is an existing file but i i am redirected on my index.php...
Is there something i did wrong ?
here is my vhost config
<VirtualHost *:*>
ServerAdmin me#local.loc
DocumentRoot "c:/wamp/www/Covoiturage"
ServerName covoiturage.loc
ErrorLog "logs/covoiturage-error.log"
CustomLog "logs/covoiturage-access.log" combined
<directory c:/wamp/www/covoiturage/>
Allow from all
AllowOverride all
</directory>
</VirtualHost>
here is my htaccess :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteRule ^ index.php [L]
You are adding RewriteCond to wrong RewriteRule. RewriteCond only in effect to very next RewriteRule. Try this code:
RewriteEngine On
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Deny from all folders except /index.php and two folders

This is an odd configuration, I'll give you that, but I'm looking to deny access to all folders in our system but it must be able to access,
/index.php (Wordpress bootloader)
/wp-*/* (e.g. wp-content, wp-admin, etc.)
/templates/* (some of our templates and custom content)
Everything else is denied (and there are hundreds of folders).
The problem I have is allowing index.php and 2 folders and then denying everything else inside /.
I should be able to accept,
http://example.com/
http://example.com/index.php
http://example.com/wp-content/...
http://example.com/wp-admin/...
http://example.com/wp-includes/...
http://example.com/templates/template.css
http://example.com/templates/subfolder/js/file.js
and reject,
http://example.com/thisIsAFolder
http://example.com/thisIsAnotherFolder
This is what I have,
<VirtualHost *:80>
ServerName example.com
DirectoryIndex index.php
DocumentRoot /var/www/
<Directory />
Order deny,allow
Deny from all
</Directory>
<Files index.php>
Allow from all
</Files>
<Directory "/var/www(/|/wp*/|/templates/)">
Allow from all
Options FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) wp/$2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ wp/$2 [L]
RewriteRule . index.php [L]
</Directory>
</VirtualHost>
But I continually receive Red Hat Enterprise Linux Test Page (using CentOS) and it allows for sub-directories because I allow /.
EDIT
This is the Apache config that ended up working for me. Big thanks to Jon Lin for the help.
<VirtualHost *:80>
ServerName example.com
DirectoryIndex index.php
DocumentRoot /var/www/
# first, deny all access
<Directory />
Order deny,allow
Deny from all
</Directory>
# then start to allow access where required
<Files index.php>
Allow from all
</Files>
<Directory "/var/www/">
Allow from all
Options FollowSymLinks
RewriteEngine On
RewriteBase /
# go directly to index.php if it is accessed
RewriteRule ^index\.php$ - [L]
# re-write URL for wp-admin access
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
# re-write wp-* access to route through wp/
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(content|admin|includes).*) wp/$2 [L]
# re-write all .php files to route through wp/
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ wp/$2 [L]
# go directly to real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# respond to all other URLs by passing them through index.php
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
# deny access if URL doesn't start with these
RewriteCond %{REQUEST_URI} !^/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/wp-[^/]+/
RewriteCond %{REQUEST_URI} !^/templates/
RewriteRule ^ - [L,F]
</Directory>
</VirtualHost>
You could try adding:
RewriteCond %{REQUEST_URI} !^/(index\.php)?$
RewriteCond %{REQUEST_URI} !^/wp-[^/]+/
RewriteCond %{REQUEST_URI} !^/templates/
RewriteRule ^ - [L,F]
right before:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
The problem here though, is that permalinks will probably break, because a request like: /posts/post-title/ will not match an of the "allowed" URI's. If that's a problem, then move it to right before this line:
RewriteRule . index.php [L]

Unable to get .htaccess working

As an experienced web developer, I feel like an idiot posting this. Somehow I tend to have issues with .htaccess. I'm trying to route all requests within /wiki to my index.php with the following...
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ./ /index.php [L]
Virutal host:
<VirtualHost *:80>
ServerName mysite.local
DocumentRoot "/var/www/mysite/public_html"
<Directory "/var/www/mysite/public_html">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
I'm trying to access http://mysite.local/wiki/asdf and getting a 404 error.
Apache's error log shows nothing
If /wiki is an existing folder in your public_html root folder, and your index.php file is in the wiki folder, then you can give the following a try:
RewriteEngine On
RewriteBase /wiki
RewriteRule index\.php - [F,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule .* index.php [L]
If the wiki folder does not exist, and you're doing all the work at the root:
RewriteEngine On
RewriteBase /
RewriteRule index\.php - [F,L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule wiki/(.*) index.php/$1 [L]
Now give this a try! To rewrite non-existence /wiki and /wiki/ and /wiki/$var into /index.php:
RewriteCond %{REQUEST_URI} ^/wiki/?
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) /index.php
I ended up needing what the Yii Framework uses...
RewriteEngine On
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php

example.com/dir/index.php works but example.com/dir/ doesn't

I'm on Linux CENT OS 6.3 with latest php and latest apache,
on httpd.conf I got
DirectoryIndex index.php
<Directory "/var/www/html">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
When I go to:
example.com/mydir/index.php
it works, but it does not work for example.com/mydir/
it is a problem to me.
Actually might it have to do with something in the .htaccess file:
RewriteEngine on
options +FollowSymLinks
RewriteRule ^([A-Za-z-0-9-_.]+)/pics/([A-Za-z-0-9-_.]*.*)?$ users/$1/pics/$2
#levanta de users/pics
RewriteRule ^([A-Za-z-0-9-_.]+)/vids/([A-Za-z-0-9-_.]*.*)?$ users/$1/vids/$2
#levanta de users/vids
RewriteCond %{REQUEST_URI} ^.*\.(html|png|jpg|gif|jpeg|css|js)
RewriteRule ^([^/]+)(/(.*))?$ - [L]
RewriteCond %{REQUEST_URI} !settingsd
RewriteRule ^settings settingsd/settings.php
RewriteRule ^lists/ master/index.php
RewriteRule ^lists/(.*)$ $1
RewriteRule ^index\.(php) master/index.php
#levanta de master/
RewriteCond %{REQUEST_URI} !.*\.(css|js|php)|pokes|settings|subgram
RewriteRule ^([A-Za-z-0-9-_.]+)?$ master/$2
RewriteCond %{REQUEST_URI} !pokesd
RewriteRule ^pokes(.*)$ pokesd/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !pokes|subgram
RewriteRule ^([A-Za-z-0-9-_.]+)/([A-Za-z-0-9-_.]+.php)?$ master/$2
#levanta de master/
RewriteRule ^messages/ messages.php
RewriteRule ^notes/me/ notes/notes.php
RewriteRule ^notes/drafts/ notes/notes_drafts.php
RewriteRule ^notes/tagged/ notes/notes_tagged.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !photos_albums|photos_stream
RewriteRule ^([A-Za-z-0-9-_.]+)/photos $1/view_photos.php
RewriteRule ^([A-Za-z-0-9-_.]+)/photos_albums $1/view_photos.php?sk=photos_albums
RewriteRule ^([A-Za-z-0-9-_.]+)/photos_stream $1/view_photos.php?sk=photos_stream
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([A-Za-z-0-9-_.]+)/friends $1/view_friends.php
RewriteCond %{REQUEST_URI} !listsd
RewriteRule ^bookmarks/lists(.*)$ bookmarks/listsd/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^v(.*)$ v/video_embed.php?sbid=$1
RewriteCond %{REQUEST_URI} !.*\.(css|js|php)|notes/me/|notes/drafts/|notes/tagged/
RewriteRule ^notes/ notes/
RewriteRule ^r.php$ gvrrgvrr45.php$1
It has to do with a rule in there because when I strip it all and upload to the server clean it suddenly works, that same .htaccess was working in Windows but isn't working in this config.
It's not related to .htaccess . It's your Apache setting ( /etc/httpd.conf )
Check your DirectoryIndex directive settings. It should have at least index.php to support your needs :
DirectoryIndex index.html index.htm index.shtm index.shtml index.php
or remove any entries that does not fit your need.