.htaccess RewriteRule trouble - apache

I am guessing this question has been asked many times, but i could not find one that would perhaps give me what I need.
So.. I can access the scripts by the following url's:
http://website.com/index.php/hello/world
http://website.com/hello/world
Both go to index.php which parses the input (hello/world in this example).
this is my .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1&%{QUERY_STRING} [L]
</IfModule>
However. When the site is accessed like this:
http://website.com/index.php/hello/world
the RewriteRule outputs something similar to index.php?path=index.php/hello/world
I want to remove that index.php after path= in the RewriteRule

Your .htaccess file should look like this (notice the new rule that checks if index.php is a part of url):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index.php/(.*)$ index.php?path=$1&%{QUERY_STRING} [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1&%{QUERY_STRING} [L]
</IfModule>

Try this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Modifying answer from Stackoverflow answer to suit this question
The .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Redirect user
RewriteCond %{THE_REQUEST} ^.*index.php.*
RewriteRule ^(.*)index.php(.*)$ $1$2 [NC,R=301,L]
# Handle the query to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?path=$1&%{QUERY_STRING} [L]
</IfModule>

Related

issue redirecting http to https

I'm trying to enforce https for all traffic and I've tried to add the following lines to .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]`
The issue is that the current .htaccess file already has the following code:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [NC,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
I combined both as follows:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteRule ^(.*)$ index.php [NC,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Unfortunately, this is not working out. I'm getting a redirect loop error... Thanks in advance for your help.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} =off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^ index.php [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

Append query string to all URLs with .htaccess

For testing purposes, I would like to append a query string (let's say testing=true) to any url requested from my website.
E.g. I want to change
example.com
example.com/product
example.com/search?q=string
to
example.com?testing=true
example.com/product?testing=true
example.com/search?q=string&testing=true
I tried this:
RewriteRule ^/?(.*) http://example.com/$1?testing=true [L,R,NE]
but it breaks the site. Not an expert.
Any ideas?
EDIT:
I should add I'm working with CodeIgniter (PHP) and my current .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
You can use this new rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)testing=true(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?testing=true [L,QSA,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

ModRewrite remove /index.php/

I've got old urls and want to remove the /index.php/ via .htaccess:
So old url is:
www.mysite.com/index.php/onesite.html
rewrite in .htaccess into
www.mysite.com/onesite.html
How can I do that?
Thx for your help
Adding the following code to your htaccess will allow this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index.php/(.*)$ www.mysite.com/$1 [R=302,L]
Changing 302 to 301 when you are sure the redirect is working
Try this one:
RewriteRule ^index.php/(.*?)$ http://%{HTTP_HOST}/$1 [R=301,L]
Just add this in your .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
If you site is in subfolder then
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /shop/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shop/index.php [L]
</IfModule>
You can use these 2 rules in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^index\.php(/.*)?$ $1 [L,R=301,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /index.php/$1 [L]

Annoying Silex subdirectory .htaccess issue

I'm working on a few projects using Silex and on none of them can I get the .htaccess file working. The current directory structure is http://localhost/IIV/
The front file in Silex is in http://localhost/IIV/web/index.php
This is what I have currently:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /IIV/
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/web/$1 -f
RewriteRule ^(.+?)/?$ /web/$1 [L]
RewriteCond %{DOCUMENT_ROOT}/web/$1 !-f
RewriteRule ^(.+?)/?$ /web/index.php [L]
RewriteRule ^(.+?)/?$ /web/index_dev.php [L]
Your RewriteBase is wrong, try this:
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /IIV/web/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
Also, make sure Apache is configured to load the .htaccess rules as #Maerlyn mentioned above.

htaccess rewrite rule for backend/frontend app

I have a yii app that has separate back end and front end. I am trying to make friendly url.
I have tried some online tools but could not get it correct
So I want this url
http://mydomain.lc/abs/admin/some/crazy/urls (for example
some/crazy/urls can be admin/index)
will be rewritten to:
http://mydomain.lc/abs/backend.php/some/crazy/urls (because this
url works directly)
And I have also front end site but they are both in the same project so they share .httacess.
the rule for .htaccess for front end is:
this url:
http://mydomain.lc/abs/some/crazy/urls (some can not be admin
here, so we can differentiate btw fron and back end)
should be
http://mydomain.lc/abs/index.php/some/crazy/urls
I have:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^abs/admin/?(.*?)$ abs/backend.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!abs/admin\/)(.*?)$ abs/index.php?url=$1 [QSA,L]
</IfModule>
the above script is not working.
Root is under abs folder and .htaccess in the root
You do not have to change anything except RewriteBase / to RewriteBase /abs
RewriteEngine On
RewriteBase /abs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/?(.*?)$ backend.php?url=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!admin\/)(.*?)$ index.php?url=$1 [QSA,L]
I guess the problem is with the RewriteBase.
You may try this in the .htaccess file in /abs directory:
Update according to last OP comment: this one is correct: backend.php/some/crazy/urls
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /abs
# Backend admin
RewriteCond %{REQUEST_URI} !backend\.php [NC]
RewriteRule ^admin(.*) /backend.php/$1 [QSA,L,NC]
# Frontend
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteCond %{REQUEST_URI} !admin [NC]
RewriteRule ^(.*) /index.php/$1 [QSA,L,NC]