htaccess RewriteRule not executing properly - .htaccess

I'm trying to figure out why my Rewriterule doest work properly.
RewriteRule ^en/(.*).php?(.*)#(.*) $1.php?$2&language=english#$3 [NC,L,QSA]
RewriteRule ^en/(.*).php?(.*) $1.php?$2&language=english [NC,L,QSA]
RewriteRule ^en/(.*).php#(.*) $1.php?language=english#$2 [NC,L,QSA]
RewriteRule ^en/(.*).php$ $1.php?language=english [NC,L,QSA]
RewriteRule ^en/$ index.php?language=english [NC,L,QSA]
I can currently see english version by using www.example.com/?language=english
but when I'm tring to open www.example.com/en I see 404 page.

Try with:
RewriteRule ^en/(.*)\.php $1.php?language=english [NC,L,QSA]
RewriteRule ^en/?$ index.php?language=english [NC,L,QSA]

Related

Htaccess add and force trailing slash

I am having some trouble with my htaccess code:
RewriteEngine on
RewriteRule ^about\/$ /about.php [L]
RewriteRule ^about?$ /about/ [L,R]
RewriteRule ^contact\/$ /contact.php [L]
RewriteRule ^contact?$ /contact/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery\/$ /loc-gallery.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery?$ /$1/gallery/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/location\/$ /loc-location.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/location?$ /$1/location/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/contact\/$ /loc-contact.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/contact?$ /$1/contact/ [L,R]
RewriteRule ^([A-Za-z0-9\-]+)\/$ /loc-home.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)?$ /$1/ [L,R]
The problem is that if i want to access:
http://example.com/about
http://example.com/contact
i am not redirected to
`http://example.com/about/` and `http://example.com/contact/`
and when i try to access http://example.com/ i am redirected to http://example.com//
What i am doing wrong? Thanks in advance!
When you try to access http://example.com/ i am redirected to http://example.com// is happening because of this line :
RewriteRule ^([A-Za-z0-9\-]+)?$ /$1/ [L,R]
It will match the empty URI because of this regex ^([A-Za-z0-9\-]+)?$
it means match if ([A-Za-z0-9\-]+) then or not ? .
It should look like this :
RewriteRule ^([A-Za-z0-9\-]+)$ /$1/ [L,R]
I also sumerized your code and fixed some issues like this :
RewriteEngine on
RewriteRule ^about(\/)?$ /about.php [L]
RewriteRule ^contact(\/)?$ /contact.php [L]
RewriteRule ^([A-Za-z0-9\-]+)\/gallery(\/)?$ /loc-gallery.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/location(\/)?$ /loc-location.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/contact(\/)?$ /loc-contact.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)\/$ /loc-home.php?slug=$1 [L]
RewriteRule ^([A-Za-z0-9\-]+)$ /$1/ [L,R]
Clear browser cache and test it , if it is ok change R to R=301 to get permanent redirection

all pages are loading except index page?

I've a htaccess code here.All the pages will work with redirect except the home page. I made changes in conf file also but no use please help. It says object not found when I click the home page link.
DirectoryIndex index.php
RewriteEngine on
AddDefaultCharset UTF-8
RewriteCond %{HTTP} =on
RewriteRule ^home/?$ index.php [NC,L,QSA]
RewriteRule ^about-us/?$ about.php [NC,L,QSA]
RewriteRule ^quick-service/?$ quick_service.php [NC,L,QSA]
RewriteRule ^road-side/?$ road_side.php [NC,L,QSA]
RewriteRule ^video-visualization/?$ video_visual.php [NC,L,QSA]
RewriteRule ^distance-approach/?$ distance_approach.php [NC,L,QSA]
RewriteRule ^pickup-drop/?$ pickup_drop.php [NC,L,QSA]
RewriteRule ^checking-status/?$ checking_status.php [NC,L,QSA]
RewriteRule ^genuine-parts/?$ genuine_parts.php [NC,L,QSA]
RewriteRule ^book-vehicle/?$ book_vehicle.php [NC,L,QSA]
RewriteRule ^Sign-In/?$ login.php [NC,L,QSA]
RewriteRule ^Register/?$ register.php [NC,L,QSA]
RewriteRule ^purchase-parts/?$ purchase.php [NC,L,QSA]
RewriteRule ^contact/?$ contact.php [NC,L,QSA]
RewriteRule ^service-centers/?$ service_centers.php [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Mod_rewrite conflict?

I have a problem with my mod_rewrite rules that I cannot manage to figure out myself!
Essentially I have a directory of countries (100+) that will display (#worldwide) the country information depending on the URL.
I also have a fishing clubs directory that I have instructed .htaccess to ignore this rule. This seems to be working ok for the base directory, however I cannot get it to redirect /fishing-clubs/name-of-country to /fishing-clubs/index.php?country=COUNTRY
website.com//fishing-clubs/ is going to the /fishing-clubs/ directory.
However website.com/fishing-clubs/name-of-country/ is giving a 404 error.
Any idea what is wrong with my code?
RewriteRule ^fishing-clubs/ - [L,NC]
RewriteRule ^fishing-clubs/([A-Za-z0-9-\s_()]+)/?$ /fishing-clubs/index.php?country=$1 [NC]
#worldwide
RewriteRule ^([A-Za-z0-9-\s_()]+)/?$ country-admin-city.php?country=$1 [NC]
RewriteRule ^([A-Za-z0-9-\s_()]+)/([A-Za-z0-9-\s_()]+)/?$ country-admin-city.php?country=$1&admin1code=$2 [NC]
RewriteRule ^([A-Za-z0-9-\s_()]+)/([A-Za-z0-9-\s_()]+)/([A-Za-z0-9-s_()]+)/?$ country-admin-city.php?country=$1&admin1code=$2&admin2code=$3 [NC]
RewriteRule ^([A-Za-z0-9-\s_()]+)/([A-Za-z0-9-\s_()]+)/([A-Za-z0-9-s_()]+)/([A-Za-z0-9-s_()]+)/?$ country-admin-city.php?country=$1&admin1code=$2&admin2code=$3&city=$4 [NC]
Have it like this:
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^fishing-clubs/?$ - [L,NC]
RewriteRule ^fishing-clubs/([\w()-]+)/?$ /fishing-clubs/index.php?country=$1 [NC,L,QSA]
#worldwide
RewriteRule ^([\w()-]+)/?$ country-admin-city.php?country=$1 [NC,L,QSA]
RewriteRule ^([\w()-]+)/([\w()-]+)/?$ country-admin-city.php?country=$1&admin1code=$2 [NC,L,QSA]
RewriteRule ^([\w()-]+)/([\w()-]+)/([\w()-]+)/?$ country-admin-city.php?country=$1&admin1code=$2&admin2code=$3 [NC,L,QSA]
RewriteRule ^([\w()-]+)/([\w()-]+)/([\w()-]+)/([\w()-]+)/?$ country-admin-city.php?country=$1&admin1code=$2&admin2code=$3&city=$4 [NC,L,QSA]

Unable to get variable from htaccess rewrite url

I am unable to get variable from htaccess url
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^.]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]
#redirect localhost/apnaujjain/page.php?page_id=1&album_id=1&action=contacts to localhost/apnaujjain/1/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^.]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
Now my final url should be
loalhost/apnaujjain/1/1/contacts.html and all parameters should be get individualy
Now url is creating ok but I am unable to get album id via url its coming with third variable action but not album id. Can anyone suggest what I am doing wrong here... thanks in advance
Your regex in 2nd rule needs some tweaking:
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&album_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2/%3.html? [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/([^./]+)\.html$ page.php?page_id=$1&album_id=$2&action=$3 [NC,L,QSA]
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+)\s [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^./]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

Condense mod rewrite

Is there any way to condense this into one line:
RewriteRule \.html+(.*)$ index.php?$1 [L]
RewriteRule \.htm+(.*)$ index.php?$1 [L]
I have tried:
RewriteRule \.(html|htm)+(.*)$ index.php?$1 [L]
But it didn't have the desired effect. Basically I want to check if either the .html and .htm extension is part of the url.
Thanks
Why do you have that plus sign there? It should be like this:
RewriteRule \.html(.*)$ index.php?$1 [L]
RewriteRule \.htm(.*)$ index.php?$1 [L]
and condensed:
RewriteRule \.html?(.*)$ index.php?$1 [L]
or:
RewriteRule \.(html|htm)(.*)$ index.php?$1 [L]

Resources