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]
Related
For a website I have locally, I need to set the display language as if it were a subfolder, using an htaccess file.
For example, if I choose to display the site in German, instead of displaying it like this => local_ip/test/index.php?lang=de, I would like it to be displayed like this => local_ip/test/de/ .
Also, if I type in the url without setting the language, I would like htaccess to automatically redirect to the German language, from so => local_ip/test/tour/mountain/ to so => local_ip/test/de/tour/mountain .
My current htaccess file is structured as follows:
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&p=%1 [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule ^city/(.+?)/?$ city.php?url=$1&t=%1&p=%2 [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule ^city/(.+?)/?$ city.php?url=$1&c=true [NC,L]
RewriteRule ^city/(.+?)$ city.php?url=$1 [NC,L]
RewriteRule ^tour/(.+?)$ tour.php?url=$1 [NC,L]
SOLVED
After several attempts and after reading several support requests posted in this community, I found a way to make the system recognise all the other rules as well. All I had to do was put them before the last rule provided by the user who supported me. Here is the final code.
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
RewriteRule ^de/$ /test/index.php?lang=de [L]
RewriteCond %{QUERY_STRING} ^t=(.+?)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&p=%1&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^t=(.+?)&p=([0-9]+)
RewriteRule city/(.+?)/?$ city.php?url=$1&t=%1&p=%2&lang=de [NC,L]
RewriteCond %{QUERY_STRING} ^c=true
RewriteRule city/(.+?)/?$ city.php?url=$1&c=true&lang=de [NC,L]
RewriteRule city/(.+?)$ city.php?url=$1&lang=de [NC,L]
RewriteRule tour/(.+?)$ tour.php?url=$1&lang=de [NC,L]
RewriteRule ^de/(.*)$ /test/$1 [L]
You can use the following :
RewriteEngine on
#redirect URLs to include /de
#RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/de/ [NC]
RewriteRule (.*) /test/de/$1 [R,L]
# /test/index.php?lang=de to /test/de
RewriteRule ^de/$ /test/index.php?lang=de [END]
RewriteRule ^de/(.+)$ /test/$1 [END]
I'd like to use .htaccess for url rewrite.
In my previous post you have shown me how to do it to the index.php page but now would like to do also for other pages like browser.error.php, etc.
To do so I tried to add the last 2 rows in the .htaccess, but unfortunately if I try the link www.mysite/browser-error/ it doesn't work - Return me in the index page.
Options +FollowSymLinks
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]
RewriteRule ^(.*)/(.*)\/?$ index.php?op=$1&idric=$2 [L,QSA,NC]
RewriteRule ^(.*)\/?$ index.php?op=$1 [L,QSA]
#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r=$1 [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]
Where I am doing wrong? How would I go? Thank you
I would suggest avoid use of .* and use [^/]+ instead with re-ordering of your rules:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
#NEW ADDED ROWS
RewriteRule ^browser-error/([^/]+)/?$ browser.error.php?r=$1 [L,QSA,NC]
RewriteRule ^browser-error/?$ browser.error.php [L,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?op=$1&idric=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?op=$1 [L,QSA]
You have rules before preceding this rule. That makes your petition change to index.php before your rule is reached. (They all have the L flag that makes them be the LAST rule processed). As they are not restricted to not be applied when url is browser-error, they will run on every ///* or simialr petition.
Then, you should add RewriteCond to negate the browser-error url in your other RewriteRules as this:
Please try, and comment if that´s not what you need of it does not work as expected.
Hope it helps
Options +FollowSymLinks
RewriteEngine on
RewriteRule \.(css|jpe?g|gif|png|js|ico)$ - [L,NC]
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^(.*)/(.*)/(.*)\/?$ index.php?op=$1&idric=$2&sp=$3 [L,QSA,NC]
RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)/(.*)\/?$ index.php?op=$1&idric=$2 [L,QSA,NC]
RewriteCond %{REQUEST_URI} !^browser-error\/?(.*)?\/?%
RewriteRule ^(.*)\/?$ index.php?op=$1 [L,QSA]
#NEW ADDED ROWS
RewriteRule ^browser-error/(.*)\/?$ browser.error.php?r=$1 [L]
RewriteRule ^browser-error\/?$ browser.error.php [L]
I have a page which has a link as:
http://www.example.com/listings.php?packageType=luxury-travel
Which I'd like to send to:
http://www.example.com/luxury-travel
And also:
http://www.example.com/listings.php?packageType=luxury-travel&packageTypeSub=remote-luxury-retreats
To go to:
http://www.example.com/luxury-travel/remote-luxury-retreats
I have used:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
Which ends up affecting other pages which I have such as:
http://www.example.com/about-us
I have also tried placing:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before. Also tried playing with:
RewriteCond %{QUERY_STRING} ^packageType=([^&]+) [NC,OR]
RewriteCond %{QUERY_STRING} &packageType=([^&]+) [NC]
RewriteRule ^([^/]*)$ /listings.php?packageType=$1 [L]
Which just give me a 404 error on both links. Anyone have any ideas? Would have thought this would have been a simple one!
In your .htaccess file
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^\.*]+)/([^\.*]+)/?$ listings.php?packageType=$1&packageTypeSub=$2 [L,QSA]
RewriteRule ^/?([^\.*]+)/?$ listings.php?packageType=$1 [L,QSA]
Try this:
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC,L]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
Note that if only packageType is available, the rules would redirect to http://www.example.com/luxury-travel/ (note the / at the end) to help differentiate it from links like http://www.example.com/about-us.
If that's not an option, then you would have add rules that specifically provide page/link names.
I have edited and removed some of the development code as it wasn't working.
After a lot of headscratching I finally found a solution which works:
# Rewrite specific pages - listings
# Allow packageType
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+) [NC]
RewriteCond %{QUERY_STRING} !&packageTypeSub=([^&]+)
RewriteRule ^ /%1/? [R=301,NC,L]
# Allow packageTypeSub
RewriteCond %{THE_REQUEST} \ /+listings\.php\? [NC]
RewriteCond %{QUERY_STRING} (?:^|&)packageType=([^&]+)&packageTypeSub=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=301,NC,L]
# Ignore the images folder allowing images to display
RewriteCond %{REQUEST_URI} !(upload-images) [NC]
RewriteRule ^([^/]+)/([^/]+)?$ /listings.php?packageType=$1&packageTypeSub=$2 [L]
The main issue was the upload-images folder which contains all the images for the site packages was being blocked. I managed to exclude this folder from the rewrite rules and it now seems to be working. Thanks all for your help. Hope this helps someone else in future.
I'm having a problem with my .htaccess mod_rewrite. I've made a simple custom CMS and i've put this in a sub directory of my domain; http://www.example.com/cms
I've got the following situations:
cms/index.php?page=modules/pages/index.php
convert to
cms/modules/pages/index
and
cms/index.php?page=modules/pages/edit.php?id=1
convert to
cms/modules/pages/edit/1
I've got it working with a subdomain, but when i use example.com/cms it doesn't do anything
i've made this .htaccess but i couldn't get it working...
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
RewriteRule ^/cms/([^/]*)/$ cms/index.php?page=$1 [L,QSA]
RewriteRule ^/cms/([^/]*)$ /cms/index.php?page=$1 [L,QSA]
RewriteRule ^/cms/pages/([^/]*)/$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/cms/pages/([^/]*)$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/cms/modules/([^/]*)/([^/]*)/$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/cms/modules/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/cms/modules/([^/]*)/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2&id=$3 [L,QSA]
If this is in an htaccess file, the URI's have their prefix removed (the leading slash), so those patterns won't match anything. Either remove the leading slash from the pattern or make it optional (with a ?):
RewriteRule ^/?cms/([^/]*)/$ cms/index.php?page=$1 [L,QSA]
RewriteRule ^/?cms/([^/]*)$ /cms/index.php?page=$1 [L,QSA]
RewriteRule ^/?cms/pages/([^/]*)/$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/?cms/pages/([^/]*)$ /cms/index.php?page=pages/$1 [L,QSA]
RewriteRule ^/?cms/modules/([^/]*)/([^/]*)/$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/?cms/modules/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2 [L,QSA]
RewriteRule ^/?cms/modules/([^/]*)/([^/]*)/([^/]*)$ /cms/index.php?page=modules/$1/$2&id=$3 [L,QSA]
Additionally, your first rule can be simplified to just:
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^ - [L]
I am attempting to get the following results with an .htaccess file.
URL: Behavior(rewrite to):
example.com/x/ view-test.php?page=x
example.com/x/y view-test.php?page=y&parent=x
example.com/x/y/z view-test.php?page=z&parent=y&grandparent=z
I have the following .htaccess rules, which also include some folders which needs to ignore this scheme:
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
#RewriteRule ([^/]+)$ /view-test.php?page=$1 [L]
however, whenever I uncomment the last two lines I get an internal server error. the parent/page and grandparent/parent/page structures work fine. How can I re-write this last rule (and the preceding rules if nessecary) to achieve the desired result for the first pattern (just a page)?
# remove slash at the end to prevent redirect loop
RewriteCond %{REQUEST_URI} ^/(OIE|admin|images|scripts|css|fonts|view-test\.php) [NC]
RewriteRule ^ - [L]
RewriteRule ([^/]*)/([^/]*)/([^/]+)/?$ /OIE/view-test.php?grandparent=$1&parent=$2&page=$3 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]*)/([^/]+)/?$ /OIE/view-test.php?&parent=$1&page=$2 [L]
RewriteCond %{REQUEST_URI}!^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ([^/]+)/?$ /OIE/view-test.php?page=$1 [L]
Use this code instead:
RewriteCond %{REQUEST_URI} ^/(admin|images|scripts|css|fonts|view-test\.php)/ [NC]
RewriteRule ^ - [L,S=3]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ view-test.php?grandparent=$1&parent=$2&page=$3 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ view-test.php?&parent=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ view-test.php?page=$1 [L,QSA]
Problem was that you were missing trailing slash / in in your last RewriteRule condition.
Try this :
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ view-test.php?page=$1
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ view-test.php?page=$1&parent=$2&grandparent=$3