htaccess is supposed to remove index.php from current URL, but instead redirects to root - apache

I've read been reading StackOverflow posts for the last 30 minutes and none of them work. Maybe there is a server setup that is preventing this from working?
I just want to remove index.php from whatever URL is typed in. For example, www.mysite.com/blah/blah/index.php would become www.mysite.com/blah/blah/. www.mysite.com/index.php would become www.mysite.com/.
I've read at least 10 posts and tried each one, but it ALWAYS just redirects to the root. Here is the current code I'm using that looks like it should work:
Options +FollowSymLinks
Options +Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Instead of just removing index.php from the URL, it redirects to the root of the site.

I think this is what you are looking for:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ $1/index.php [L,QSA]

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
</IfModule>

Related

How to fix mod_rewrite problem with static to dynamic content

I would appreciate if someone can help with this mod rewrite issue. For years I had this in my htaccess, apache include files and it worked just fine.
RewriteRule ^/article/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
But recently it stopped working. Browser shows "No input file specified." and access log shows 404 response code the requests that had to be fulfilled by the condition above.
RewriteEngine is on. This is what my htaccess mod_rewrite rules at the moment
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) /404error.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^/article/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
RewriteRule ^/relationships/([0-9]*).html$ /article/article.php?id=$1 [NC,L]
</IfModule>
With your shown samples/attempts, could you please try following htaccess Rules. Please make sure to place your htaccess rules file in root and clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
Options -MultiViews -Indexes
RewriteEngine On
RewriteBase /
##Rules for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
##Rules for articile and relationship uris here.
RewriteRule ^(?:article|relationships)/([0-9]*)\.html/?$ article/article.php?id=$1 [NC,L]
##Rules for 404 handling.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map)(\?|$) 404error.php [L,NC]
</IfModule>

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.

htaccess issue causing styles and images not to load

For some reason stylesheets and images are not loading on my site :(
Don't really know why I believe it may be to do with my htaccess file here is how it currently is.
UPDATE - NEW FILE
#SetEnv APPLICATION_ENV development
DirectoryIndex /public/index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) $1 [L]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f
RewriteRule ^(.+) /public/$1 [L]
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/index.php?q=$1 [L,QSA]
RewriteRule !\.(js|ico|gif|jpg|png|css|xml|xslt)$ /public/index.php
Any idea why its not loading?
It looks like there has been some cut-and-pasting: 'RewriteEngine on' should be there only once and the code from that point is causing you trouble.
The reasons for those lines are not clear without more info on the kind of site you run, so any advice from here is trial and error testing:
The last rule is commented out and looks like an effort to get the stylesheet and images (all js|ico|gif|jpg|png|css|xml|xslt files) correctly rewritten to the desired address. You can try to comment it out (get rid of the #).
The problem was fixed by rewriting the whole file to the following
DirectoryIndex /public/index.php
RewriteEngine On
Options +FollowSymLinks -MultiViews -Indexes
RewriteBase /public
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(domainUrlNoHttp)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^http://(domainUrl)$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

Having troubles with url-rewriting using .htaccess on wamp server

I'm trying to rewrite the url of a certain php file.
Aim for url: www.mysite.com/filename/topic/ or www.mysite.com/filename/topic
What works: www.mysite.com/filename and www.mysite.com/topic
I've tried all rewrite rules I know which should work, but every time I add the topic to the url I end up with a 500 internal server error
Current .htacces file
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
//No need for php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
//Rewriting the url
RewriteRule ^([a-zA-Z0-9_-]+)$ filename.php?topic=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ filename.php?topic=$1
You have conflicting rules and there is probably an issue with content negotiation with MultiViews. Also you aren't using all the flags ([L]) need to prevent continuation of rules matching.
You can't do it that way with the second set of rules and you don't need 2 rules just to have a / or not. You can make it optional. You need to do something like this.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?topic=$2 [L]
</IfModule>

Wordpress .htaccess errors

Thanks in advance to all you .htaccess guru's out there. I'm having an issue that's been bothering me for some time. I have wordpress nested inside my own site folder structure. To the user, everything is cool. But when i go to www.domain.com/blog/wp-admin, my credentials will not log in. It just keeps looping me around to the same page. If I go to www.domain.com/blog/wp-login.php directly it works. I wonder if anyone knows why this is.
The blog is located at /fe/blog/.
The .htaccess in / is:
Options +FollowSymlinks -Indexes
RewriteEngine on
RewriteRule ^(files)/?(.*) /files/$2 [NC,L]
RewriteRule ^(.*) /fe/$1 [NC]
The .htaccess in /fe is:
Options +FollowSymlinks -Indexes
RewriteEngine on
And Finally the .htaccess in the /fe/blog/ is:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
Any help would be greatly appreciated!
Try this in your /.htaccess file:
RewriteEngine on
RewriteRule !^(files|fe)/ fe%{REQUEST_URI} [NC]
And this in your /fe/blog/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
i wonder if this line is not the culprit:
RewriteRule ^(files)/?(.*) /files/$2 [NC,L]
could you try:
RewriteRule ^files/?(.*) /files/$1 [NC,L]
If not, you might also add the flag [QSA] (Query String Append)
RewriteRule ^files/?(.*) /files/$1 [NC,L, QSA]