.htaccess not working on live site but working on localhost? - .htaccess

I am using ExpressionEngine and want to remove index.php from my URL's. I have this .htaccess file saved in the root folder. It works perfectly on localhost but when I upload it to the server it doesn't work. The correct URL appears in the address bar but the page stays on homepage. Any tips?
<IfModule mod_rewrite.c>
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Make sure your AllowOverride Directive in Apache is configured to allow .htaccess files and your server has mod_rewrite installed and active.
On Mac OS X, you'll find this file at /etc/apache2/httpd.conf. Look for the <Directory> Directive and change it to be:
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
</Directory>
You'll need to restart Apache so it can read the new configuration:
sudo /usr/sbin/apachectl restart
If you'd prefer to use a GUI to restart Apache, go to Apple > System Preferences > Sharing and toggle the checkbox next to the Web Sharing service.
If you're using Windows or any flavor of Linux, the same approach applies, but the Apache configuration may be in a different place, especially if you're using WAMP or MAMP.
Also, for reference, the "officially supported method" by EllisLab for Removing index.php from ExpressionEngine URLs is the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>

Related

Is it possible to use htaccess to rewrite URLs without file extensions with MultiViews on

I'm using the following .htaccess file in order to internally rewrite URLs without the file extension, i.e. /mypage opens the content of /mypage.php or /mypage.html
Options -MultiViews
# start rewrite engine
RewriteEngine on
# internally rewrite "page" to "page.html"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
# internally rewrite "page" to "page.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
It works exactly is it should on my local WAMP server.
However on my shared hosting service it gives an Internal server Error (500).
My hosting provider told me that the error log says that "Option MultiViews not allowed here" and confirmed me that MultiViews is enabled by default.
If I comment the first line there is no Error 500 but the rewriting doesn't work anymore (because MultiViews is enabled). They said to me that they can not change it for my host.
So is it possible for me to get this .htaccess rewriting URL working with MultiViews on ?

htaccess rewrite url to hide variable

My knowledge of htaccess is pretty limited, so please be gentle...
I have a php page that displays an article, based on a variable passed through the url. For each article, I want the url to be changed to the article title, but I can't lose the article id being passed to the page...
So, for ...
www.somesite.com/news/article?id=4
... I can change it to ...
www.somesite.com/news/here-is-the-article-title
Or for
www.somesite.com/news/article?id=7
... I can change it to ...
www.somesite.com/news/some-other-title-that-I-choose
Bascially, I want the content for "article?id=4" to be shown on the page, but the URL to read "here-is-the-article-title". Is this even possible using htaccess? Or is there a similar solution? I know that I'll probably have to rewrite EACH url, and I'm fine with that.
I have some standard htaccess code I use...
Options +FollowSymLinks -MultiViews
# enable the rewrite engine
RewriteEngine On
# Set your root directory
RewriteBase /
# remove the .php extension
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index and reference the directory
RewriteRule (.*)/index$ $1/ [R=301]
# remove trailing slash if not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# forward request to html file, **but don't redirect (bot friendly)**
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Disable Directory Browsing
Options -Indexes
# Protect htaccess File
<files ~ "^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</files>
Basic REDIRECT doesn't work, since I lose the article id.

.htaccess issue | Codeigniter

I'm relatively new to Codeigniter and MVC. But, have successfully made two apps 'Locally'. While exploring, I found a way to remove 'Index.php' from the URL and also about custom routes. The .htaccess file that i have works like charm locally, but when trying to host it; there is a issue 505 internal server issue
Here is the first .htaccess code that i have (works locally) :-
<IfModule mod_rewrite.c>
# Turn on URL rewriting
RewriteEngine On
# If your website begins from a folder e.g localhost/my_project then
# you have to change it to: RewriteBase /my_project/
# If your site begins from the root e.g. example.local/ then
# let it as it is
RewriteBase /
# Protect application and system files from being viewed when the index.php is missing
RewriteCond $1 ^(application|system|private|logs)
# Rewrite to index.php/access_denied/URL
RewriteRule ^(.*)$ index.php/access_denied/$1 [PT,L]
# Allow these directories and files to be displayed directly:
RewriteCond $1 ^(index\.php|robots\.txt|favicon\.ico|public|assets|css|js|images)
# No rewriting
RewriteRule ^(.*)$ - [PT,L]
# Rewrite to index.php/URL
RewriteRule ^(.*)$ index.php/$1 [PT,L]
</IfModule>
When i use the code above there is an error. And, the app works only when .htaccess is removed. (I then have to use the inconvenient long URLs)
After a brief research and using different .htaccess without success, i asked one of my friends who has a Hosted CI app successfully running. He sends me a file which leads me the landing page without any problem; but, cant call any functions with/without using routing .i.e. If i use the custom routed URl (www.mySite.com/contact) then also it leads me to the landing page, the same with actual URL scheme (www.mySite.com/welcome/contact_page)
The new code here:-
<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>
ErrorDocument 404 /index.php
</IfModule>
Using this code shows me the landing page but i cannot navigate any further from there. When i try to call a function the landing page reloads. This, I think is because of the last error Handling Code (ErrorDocument 404 /index.php)
Does anyone know what the solution the problem might be??
questions
Why doesnt the first .htaccess code work when hosted?
What may be the issue with the second available .htaccess file??
Do you guys have better .htaccess file? If yes, can you post it here??
Try this .htaccess (Codeigniter Recommended )
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
or
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
In application/config/config.php
$config['base_url'] = 'http://stackoverflow.com/';
$config['index_page'] = ''; # Should be empty
And make sure Controller name, Model names are in proper way. Bcz Linux Host is an Case-Sensitive.

Kohana rewriting urls in .htaccess showing file not found message

I'm using Kohana 3.2 framework, I've put the standard .htaccess file in the same folder of the aplication.
my .htaccess file is
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /mysite/
# 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_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ /index.php/$0 [PT]
I've tried changing the las line to
RewriteRule ^(.*)$ /index.php/$0
RewriteRule ^(.*)$ /index.php/$0 [PT,L]
RewriteRule ^(.*)$ /index.php/$0 [PT,L,QSA]
RewriteRule .* /index.php/$0 [PT] - [PT,L] - [PT,L,QSA]
But it still shows
Not Found
The requested URL /index.php/login was not found on this server.
The complete path of the app is /var/www/es/ec/mysite
The complete working URL is http://10.0.0.1/ec/mysite/index.php/login
The complete NOT working URL is http://10.0.0.1/ec/mysite/login
Also...
Running in Apache 2.2.3 over CentOS 5
Any idea???
I got it!!
Here is how it gets done when the app is inside other folders.
The complete path of the app is /var/www/es/ec/mysite
The complete working URL is http://10.0.0.1/ec/mysite/index.php/login
The complete NOT working URL was http://10.0.0.1/ec/mysite/login
In /var/www/es/ec/mysite/application/bootstrap.php the Kohana::init stays like this
Kohana::init(array(
'base_url' => '/ec/mysite/',
'index_file' => FALSE,
'charset' => 'ISO-8859-1',
));
And the .htaccess like this
# 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_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* /ec/mysite/index.php/$0
Hope this helps some one else!
Cheers!
You can try my Kohana .htaccess file
# Set environment
SetEnv KOHANA_ENV "development"
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I use Kohana 3.2, also. This rewrite tends to work for most of my projects.
Let me know how it works out!
In your bootstrap.php inside Kohana::init set 'index_file' => '',

Unable to Remove index.php on ExpressionEngine WAMP Installation

I have WAMP v2.2 installed on my Windows 7 machine, with virtual hosts enabled.
I've successfully installed ExpressionEngine for one of my local sites, and everything works great except when I try to remove index.php from the URL using the approved .htaccess method. I still get a 404 error if index.php is not present, but the page displays fine with index.php in the URL.
I made sure the rewrite module was checked in the Apache menu:
. I've successfully used this method dozens of times on commercial hosts, so I'm stumped.
Update for pvledoux:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Note: I tried adding the ? into the rule, but it didn't work.
The site is at the root of the Virtual Host.
Got It!
I needed to add AllowOverride in httpd-vhosts.conf:
<Directory "C:\Site\Root\Path">
AllowOverride All
Order Deny,Allow
Allow from all
</Directory>
A gagillion thanks and a 6-pack of beer goes to #parhamr for helping me out of this pickle.
A few possibilities here you could try out here:
[...]
RewriteEngine On
Options FollowSymLinks
[...]
Or:
[...]
RewriteEngine On
Options +MultiViews
[...]
Or:
[...]
RewriteEngine On
Options FollowSymLinks
Options +MultiViews
[...]
From my experience, Apache when running on Windows often needs one or both of those in order to do these kinds of things correctly. It might also straight up 500 on you, but these have worked for me in the past.
try to add RewriteBase /
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
and check for sure that mod_rewrite is active in phpinfo
Have you checked your default document in Apache? If you are removing index.php physically from the machine and it reports a 404 on just a directory, www.domain.com/home/ that means Apache is looking for index.php still.
Is mod_rewrite turned on in the Apache?

Resources