Rewrite url - Rediriger sans index.php - .htaccess

my URL is of the type: www.mysite.fr and for any page: www.mysite.fr/index.php/test
I wish that www.mysite.fr/test displays www.mysite.fr/index.php/test (it's more beautiful without index.php!)
I tried this but: www.mysite.fr/test displays the home page instead of the test page
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php/(.*)\sHTTP.*$ [NC]
RewriteRule ^ /%1%2 [R=301,L]
RewriteCond %{REQUEST_URI} !index\.php
RewriteRule ^(.*)$ index.php/$1 [L]
Second line to remove index.php exteranlly.
Fifth line to redirect request to original path internally .
Note: clear browser cache the test

Your method works on my localhost but not on my online site. This is the 5th line that blocks in my opinion but I do not see why even adding a slash before index.
Someone have an idea ?

Related

HTACCESS rewriting not working for more than 2 parameters

Alright. Here is my htaccess code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ bank.php?bank=$1
#This will make my url e.g. site.com/citibank
RewriteRule ^(.*)/(.*)$ state.php?bank=$1&state=$2
#This will make my url e.g. site.com/citibank/new-york
RewriteRule ^(.*)/(.*)/(.*)$ location.php?bank=$1&state=$2&district=$3
#This will make my url e.g. site.com/citibank/midtown
PROBLEM: Its working till second rule but when it comes to third and final rule, it created url like: site.com/bank.php/citibank/new-york
Any help would be appreciated.
Thanks
It is because your first rule is using .* pattern and is matching everything. Change your code to this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ bank.php?bank=$1 [L,QSA]
#This will make my url e.g. site.com/citibank
RewriteRule ^([^/]+)/([^/]+)/?$ state.php?bank=$1&state=$2 [L,QSA]
#This will make my url e.g. site.com/citibank/new-york
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ location.php?bank=$1&state=$2&district=$3 [L,QSA]
#This will make my url e.g. site.com/citibank/midtown

Looking for advice on mod_rewrite and htaccess

I am not experienced with htaccess... But I am authoring a site on which I am using a simple redirect. I basically just copied the first htaccess that I found and it works fine, but I am curious if I am using it properly and/or if it is responsible for some problems I am having with the site.
This is my file..
RewriteEngine On
Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /src/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
The index.php (as you can tell) lives in /src), as does most pf the site.
One problem I am having is that accessing some of the includes results in pulling back the index.php!
Any advice, or directions to some tuts or articles on htaccess would be GREATLY appreciated.
Thanks
Have to say that the lot of the code in here looks redundant. Replace your code with this:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} ^/(dir1|dir2|dir3) [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1 [L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
Also when you say that ccessing some of the includes results in pulling back the index.php! I hope you are just including them like:
include('path/to/somefile.php');
That won't go through .htaccess since this inclusion is transparent to Apache.
Try combining a few of the rules:
RewriteEngine On
# Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
# block access to the htaccess file
RewriteRule ^\.htaccess$ - [F,L]
# route "/" requests to index.php
RewriteRule ^$ /src/index.php [L]
# route requests that exists in /src/ to /src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -d
RewriteRule ^(.*)$ /src/$1 [L]
# route everything else to /src/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /src/index.php [L]

404 redirect works but changes rewritten URL path in browser to full URL path

I use a URL rewriting scheme of the following:
example.com/about/
example.com/this/is/a/page/
GOES TO:
example.com/pages/about/about.php
example.com/pages/this/is/a/page/page.php
It works fine, but on a 404 error, when typing in example.com/badpage/ it shows 404 page but changes the URL string to example.com/pages/badpage/badpage.php.
How to do keep the URL the same even on a 404 error?
(The htaccess also adds on '/' to the end of the URL requested as you can see from the code below)
htaccesss code:
DirectoryIndex /pages/index/index.php
ErrorDocument 404 /pages/error/404.php
RewriteEngine On
#Removes the www from domain for SEO
RewriteCond %{HTTP_HOST} ^www\.portal\.example\.com$ [NC]
RewriteRule ^(.*)$ http://portal.example.com/$1 [L,R=301]
# Don't fix direct file links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://portal.example.com/$1/ [L,R=301]
RewriteRule ^([A-Za-z0-9_-]+)/?$ pages/$1/$1.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$2.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$3/$3.php [NC,L]
RewriteRule ^([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$ pages/$1/$2/$3/$4/$4.php [NC,L]
You need to add conditions before each of your rewrites to ensure you're not blindly rewriting into a file that doesn't exist. Mainly, the 4 rules at the end need to have some conditions:
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%1.php -f
RewriteRule ^ pages/%1/%1.php [NC,L]
The first condition creates a grouping which is backreferenced using %1. The second condition creates a path that you are trying to rewrite to and checks if that file exists using -f. Same thing for the others:
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%2/%2.php -f
RewriteRule ^ pages/%1/%2/%2.php [NC,L]
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%2/%3/%3.php -f
RewriteRule ^ pages/%1/%2/%3/%3.php [NC,L]
RewriteCond %{REQUEST_URI} ^/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/([A-Za-z0-9_-]+)/?$
RewriteCond %{DOCUMENT_ROOT}/pages/%1/%2/%3/%4/%4.php -f
RewriteRule ^ pages/%1/%2/%3/%4/%4.php [NC,L]

Force indexhibit to use url with www

i was trying to make my indexhibit installation to use nice urls + domain redirect to make it allways using 'www' in page URL but i've failed. Nice urls works fine but i can't make this www redirect work.
Here is my original htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.+) index.php
I was trying to add those lines
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]
before RewriteCond %{REQUEST_FILENAME} -f [OR], after RewriteRule ^(.+) index.php and in any possible place but it allways breaks the page. Can you please advise me how to make it properly ? Thanks in advance :)

%2520 (Double space) in URL for URL with spaces

My URL structure is like
http://www.example.com/folder/index.php?dir=dir1
To be able to access it from
http://www.example.com/folder/dir1
and at the same time redirect the 1st URL to 2nd one, my htaccess (in 'folder') is
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]
The trouble is that if any directory name in URL contains a 'space', the URL shows %2520 instead of the 'space'.Please advice in modifying the htaccess so it shows %20 or preferrably a simple 'space'?
try adding a NE on the redirect ie
RewriteRule ^ %1? [L,R=301,NE]
EDIT
Since you've read my htaccess, do you see any possiblity of shortening it further
Below are a couple of comments
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301]
#this looks redundant with last rule, and could be deleted?
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
#this says if not an existing file
RewriteCond %{REQUEST_FILENAME} !-f
#and this says if it IS an existing directory
#Is this what you wanted, or should it be not an existing directory i.e
# RewriteCond %{REQUEST_FILENAME} !-d instead
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php?dir=$1 [L,QSA]

Resources