Arabic language as get parameter not working in routes - .htaccess

Working on a codeigniter project, given routes for both english arabic languages. English parameter is working good arabic value cannot be accessed from controller
i have tried this solution in htaccess file rewrite rule by refering to this link but not working
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
$route['^(en|ar)/CarDetails'] = "Car/CarDetails";
English URL:
http://localhost/project/en/CarDetails/car-code
Arabic URL:
http://localhost/project/ar/CarDetails/رمز السيارة
Car Code is a unique code for each car fetched from DB.
I need to fetch this car code in Arabic language as get parameter from browser in my controller so that i can fetch car details based on this code.

Based on your shown samples, could you please try following once, sorry I couldn't test it, should work IMHO though.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/ar/CarDetails/(.*)/?$ index.php?rt=$1 [L,B,QSA]

Related

htaccess rewrite url query string with index as base

New poster to this place but long term user.
I have a site which uses index.php and then has a viewpost.php and categorypost.php.
The viewpost is for viewing the main posts and the categorypost is for viewing posts under a category such as "webdesign".
The view post slug is made up of query string such as:
viewpost.php?postType=services&postCategory=webdesign&postTitle=somepost
I then want that to rewrite to
website/services/webdesign/somepost
First question:
Is this the correct way to do this by using 3 files or am I creating more work than needed?
Second question:
If I'm on the correct or at least an ok path, how do I go about redirecting?
I have seen about 30 or so posts around this but finding it hard to get my head around it and have ended up with 500 response or just straight redirecting.
RewriteRule ^(.*)$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]
Any help is greatly appreciated.
If these particular links can all start with the same word, it is the simplest, in this case "website". Because it avoids mistakenly using an existing folder with a missing page.
RewriteRule ^website/([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [NC,QSA,L]
But if it's not the case, you can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ viewpost.php?postTag=$1&postCategory=$2&postTitle=$3 [QSA,L]

What is the proper way to make your url "nicer"?

recently i have tried to build some MVC application without any framework to understand MVC pattern better. Till now i have resolved every problem i have had BUT....
Pretty common thing is to make your URL looks "nicer"
For example www.somesite.com/controller/method
instead of www.somesite.com/index.php?c=1&m=2.
i achieved this simply with htacces by aiming it to a variable.
in htaccess...RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
This works perfectly fine until someone tries to rewrite variable "url".
Basically if someone types www.somesite.com/controller/method?url=1
my Application will pop up an error page because i am parsing everything after / and calling specific controllers and methods by its name (or popping up an error page if that doesnt exists).
So i would like to know if there is a better way to do this or way to avoid this behaviourThanks :)
EDIT
In last few hours i tried to find a better solutions. I thought i could put my url into Enviromental variable instead of into get variable.
So i experimented with commands like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php [E=URL:$1,QSA]
unfortunately $_SERVER["URL"] is blank......
i would be really happy if someone could help me with this piece of code :) Thanks
EDIT 2
Okay to make it clear i'll add few examples.
My current htacces looks like this
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA]
So if someone goes to www.somesite.com/foo/bar
i will check $_GET["url"] and then explode it explode("/", filter_var(rtrim($_GET["url"], "/"), FILTER_SANITIZE_URL))
Now i have array which looks like [0=>foo 1=>bar]
That means in my code that i will try to call controller called foo and then method in this class called bar
In case that the user will try to acces www.somesite.com/foo/bar/fee/faa, fee and faa will pass as a parameters to method bar in class foo.
So this was just example how does this work. My problem is as i said when someone tries to acces lets say www.somesite.com/foo?url=0. Then my script will try to handle $_GET["url"] and the result wont be foo but 0 because of ?url=0 rewrites the value of url which was originaly set in my .htacces. So my scripts will try to call controller called 0 and if that doesnt exists itt will popup error404. I have already tried to ignore this specific variable via QUERY_STRING in htacces but this seems to me like a stupid solution. For now i would like to stick with setting evniromental variable instead of get variable or if there is some better way to achieve this :) Thanks
Your current set-up relies on $_GET to obtain core information but, as you've faced, that variable is populated from user input so anyone can mess with your routing, even inadvertently.
A typical Apache configuration for a custom router looks like this (this snippet is from CakePHP/2.x):
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
As you can see is doesn't add GET variables of its own, it merely lets existing ones to pass through. Original URL is right there inside $_SERVER, you don't need to instruct Apache to copy it into the redirected URL.
I understand you want $_SERVER['REQUEST_URI'] but you can peek inside the array with the usual methods: print_r($_SERVER), var_dump($_SERVER), phpinfo()...

rewrite url and get variable

How can I get the second part of a url to be a variable, rewrite it, and provide a fancy URL.
So example www.mywebsite.com/AFL would rewrite to www.mywebsite.com/index.php?league=AFL but would still display as www.mywebsite.com/AFL.
I am using the same pages for multiple leagues. When a user signs up they establish a league name in the database and then on each page I look for the league variable to display the info.
I also have many different pages. So a user could go to www.mywebsite/AFL/schedule_teamdisplay.php?Team=ABC&Year=2014. This would rewrite to www.mywebsite.com/schedule_teamdisplay.php?league=ALF&Team=ABC&Year=2014.
The league will always be the second entry in the URL.
Thanks
You can put this code in your htaccess (which has to be in your root folder)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /index.php?league=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$2 -f
RewriteRule ^([^/]+)/([^/]+)$ /$2?league=$1 [L,QSA]

Dynamic Rewritten URL's allow multiple params if exist

We have built a search module for our eCommerce application & attempting to build SEO friendly pages.
The basic concept below works however when adding additional params to our search the rules no longer work correctly.
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)/search/([^/]*)$ search.php?controller=$1&lang=$2&name=$3&search_keyword=$4 [L]
Example of search but needs to also work with categories selection when parameter exists.
www.mysite.com/shop/lcd
Currently this is working but only supports 2 category sets as we have 2 rules and not working with the search, the problem is there could be as many as 20 category sets
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ search.php?controller=$1&lang=$2&name=$3&categories=$4=$5 [L]
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)$ search.php?controller=$1&lang=$2&name=$3&categories=$4=$5|$6=$7 [L]
www.mysite.com/shop/TV/Plasma+3D+Portable/ (TV is the main category with 3 subs)
www.mysite.com/shop/TV/Plasma+3D+Portable/Computers/Mac+PC+Laptops+Tablets/
(Computers is the main category with 4 subs combined with the above)
This needs to also work with additional param sets
www.mysite.com/shop/TV/Plasma+3D+Portable/Computers/Mac+PC+Laptops+Tablets/manufacturers/brandA/brandB/brandC/
Please if someone can suggest a better way to write these rules to support all these additional parameter requests we would really appreciate your input.
This post helped us to solve our problem
.htaccess: GET variables are lost in rewrite
The solution is very simple in our case the controller lang & name our separated from the search parameters then params=/$4 captures the rest the URL which we can now explode & parse via PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)$ /search.php?controller=$1&lang=$2&name=$3 [QSA,L]
RewriteRule ^shop/([^/]*)/([^/]*)/([^/]*)/(.*)$ /search.php?controller=$1&lang=$2&name=$3&params=/$4 [QSA,L]

Do Not Redirect for missing Folders

I have a website which is basically a book (it is connected to a database). Users are able to read chapters for ex: http://examplebook.com/20. The problem I have is that I do not want to create folders for all the chapters in the book, but rather extract the chapter number from the url and just connect to database and display the book pages. If I do not have a folder for each chapter it just redirects and shows not found. I see some websites that have this feature so I am assuming it is possible. Any help would be greatly appreciated.
Kind Regards,
Chewy.
You mean something like this?
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]
That being in the htaccess file in your document root.

Resources