Unable to get .htaccess working - apache

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

Related

Removing index.php from the URI in Codeigniter 3

I have done all the said solutions to remove 'index.php' from the URI. The procedure I took is as below:
In .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
In config.php
the base_url is set to
$config['base_url'] = 'https://trawellmate.com/';
Removed index.php from
$config['index_page'] = '';
These are the suggested solutions. But nothing worked. I am using SSL and all non requests are redirected to https://trawellmate.com. Following is the lines added to achieve in .htaccess:
#FORCE NON-WWW REDIRECT
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule (.*) https://%1%{REQUEST_URI} [L,R=301]
Earlier with non SSL(in development mode in localhost) it was working fine. Since, we are about to go live, a quick help will be deeply appreciated.
You may try to use the following rules. It worked for me.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteCond %{HTTP_HOST} ^yourwebsite\.com$ [NC]
RewriteRule ^ https://www.yourwebsite.com%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /index\.php(/[^\ ]*)?\ HTTP/
RewriteRule ^index\.php(/(.*))?$ yourwebsite.com/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Allow overriding htaccess in Apache Configuration (Command)
sudo nano /etc/apache2/apache2.conf
and edit the file & change to
AllowOverride All
Below .htaccess configuration works for me
RewriteEngine on
RewriteBase /<app_directory>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /<app_directory>/index.php/$1 [L]
I've placed the .htacess file in /var/www/html ie. Parallel to application directory
Hope you have enabled mod_rewrite module for apache, Also add below block in virtualhost file(000-default.conf) of apache. Please restart server after changes.
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>

Unable to remove index.php from Codeigniter 3.0 URL

I am unable to remove index.php from CodeIgnitor 3.0 URL. I have applied so many solutions but nothing worked. I am using WAMP 2.5 . rewrite_module is active in Apache . $config['index_page'] = ''. My htaccess file which is in the root folder is as follows
RewriteEngine on
RewriteBase /Reporting/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|js|img|css|captcha|robots\.txt)
RewriteRule ^(.*)$ /yourfolder/index.php/$1 [L]
Reporting is the folder in www where all the files are there.
I had problems with removing index.php with the default .htaccess code, but I managed and this is how my .htaccess looks like now.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
<Files "index.php">
AcceptPathInfo On
</Files>
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>
Try with this code in .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You have this in Codeigniter documentation - READ

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.

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.

htaccess to rewrite wordpress subdirectory with permalinks to root

I have a wordpress site installed in the directory "wp". I want to keep it in that folder for maintenance purposes. Wordpress gives the option to create a htaccess for permalinks. You can see that code (which works) below.
What I want is a rewriterule so - to visitors - the site appears to be in the root. I've to change the rewritebase, but that didn't work.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wp/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wp/index.php [L]
</IfModule>
1) in your dashboard, go to settings -> general and make sure
a) wordpress directory -> http://mydomain.com/wp
b) site address -> http://mydomain.com
2) move your index.php from subdirectory to the root (MOVE, don't just copy)
3) edit your index.php to read
/** Loads the WordPress Environment and Template */
require('./wp/wp-blog-header.php');
where "wp" is your subdirectory
4) delete any .htaccess file in the subdirectory
5) add this to your .htaccess in the root
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Under this setup, your wordpress installation will be located in /wp/ directory. Visitors will visit your site using http://mydomain.com. Good luck.
What would you think of that :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /wp/index.php [L]
RewriteCond %{REQUEST_URI} !^/wp/.*$
RewriteRule ^(.*)$ /wp/$1 [L]
You rewrite all non existing things to /wp/index.php, and all non /wp/ file to a /wp/ equivalent file. So if you call index.php it's in fact /wp/index.php
For a site with two WordPress blogs, one at the site root, and one in a subfolder, i.e. https://example.com/ and https://example.com/otherblog/, here's what i did
<Directory /data/sites/example.com/otherblog>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /otherblog/index.php [L]
</IfModule>
</Directory>
<Directory /data/sites/example.com>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
</Directory>