rewritecond not working for existing files - apache

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]

Related

Apache how to write url if mssing language prefix

I have single page application written in Angular I am trying to achive something like this:
https://mypage.com/path/to/somewhere?key1=val1 -> https://mypage.com/de/path/to/somewhere?key1=val1
So if url is missing /en/ or /de/ prefix than it should append to the begging of path the prefix /de/.
The problem is that I am trying to solve is that we switch to multilanguage application recently and in old emails users have path without /en/ or /de/ paths.
In /www/data directory I have de and en directories.
This is my Apache configuraiton:
content_copy
<VirtualHost *:80>
ServerName localhost
DocumentRoot /www/data
<Directory "/www/data">
RewriteEngine on
RewriteBase /
RewriteRule ^../index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (..) $1/index.html [L]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ /de/ [R]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^$ /en/ [R]
RewriteCond %{HTTP:Accept-Language} !^de [NC]
RewriteRule ^$ /en/ [R]
</Directory>
</VirtualHost>

Codeigniter mod-rewrite in apache, removing index.php

So I am quite new to codeigniter and I have been trying to get my head around removing the index.php from my local site, I have managed to make it work for the homepage after looking through other similar questions on SO but none have got it fully working. Whenever I visit another page on my site it displays the wamp page on my screen. Here is my code in codeigniter/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# !IMPORTANT! Set your RewriteBase here and don't forget trailing and leading
# slashes.
# If your page resides at
# http://www.example.com/mypage/test1
# then use
# RewriteBase /mypage/test1/
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
I have also put in my apache/conf/httpd.conf
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
But still no luck, is there something I've missed? Thanks in advance
Follow the following step:
1. Change the config file like this:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.Use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note: .htaccess vary depending on server. I some server (e.g.: Godaddy) need to use the following .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Use this .htaccess works on most servers
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
# RewriteCond %{HTTPS} off
# RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You can uncomment the last 2 lines to force HTTPS.
Also this could also work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foldername/
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Note line 3 of the 2nd .htaccess RewriteBase /foldername/ use this RewriteBase / if your script can be reached without including a folder name.

Apache not showing index.html on address bar

I would like to show my index.html file on address bar. When you go to http://www.example.com/ to redirect it to http://www.example.com/index.html and show that index.html on address bar.
EDIT
This is my .htaccess file. I have try neo example but with no luck.
DirectoryIndex naslovnica.php
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* 404.html [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /naslovnica\.php
RewriteRule ^naslovnica\.php$ http://example.com/naslovnica.php [R=301,L]
You will have to look into Apache DocumentRoot Directory. Have a look at the following Apache Docs...
Apache DocumentRoot
You will have to create the index.html and put it in the DocumentRoot directory.
From the DOCS...
If your DocumentRoot "/usr/web" then an access to http://my.example.com/index.html refers to /usr/web/index.html.
Edited
The following document explains about hiding the index. Guess you can use it to unhhide the index as well.
Mod_Rewrite – Hide index.php
The following might be a start towards finding the right solution...
Options +FollowSymLinks
RewriteEngine on
# Redirect client requests for www.example.com/ to "www.example.com/index.html" in main doamin
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html
RewriteRule ^index\.html$ http://www.example.com/index.html [R=301,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.