Rewriting url having more than one url parameter - php

Has been researching on how to rewrite url using htaccess file. After much time invested, I still have very vague idea of how a url will be rewritten.
Basically, I have a url:
http://mehthesheep.com/bali/jimbaran/blue-point/1
I would like to rewrite to:
http://mehthesheep.com/countries/hotel.php?hotel_id=1&state=bali&city=jimbaran
How do I go about having more than one parameter in the url?
Any help with some simple explanation would be appreciated!

So apache isn't my strongest skill, but from what I understand the way you would rewrite the urls is with mod rewrite, and in your regex, each matching parameter can be mapped to something else. So something like:
#1st field #match 2
RewriteRule ^/hotel/(.*)/(.*) http://www.example.com/hotel.php?hotel_id=$2&state=$1 [NC,QSA,L]
#2nd field #match 1
My syntax might be wrong so I apologize, but the gist is that I can take patterns and then correspond the matches to '$n' keys in my rewrite url. In the above example a call to /hotel/bali/1 would rewrite to hotel.php?hotel_id=1&state=bali. Not sure if it matters, but I used a very simple match '(.*)' basically means anything.
The last bit w/ are flags you can add to make sure you get the desired behavior. QSA allows for a querystring to be attached in case you had tracking or other params come in, NC makes the matching case insensitive, in the event they try something like /Hotel. L is for last, meaning if the rule is matched this is the last rule to evaluate so execute the rewrite (or something to that effect).
Hope that helps. This really helped me dig into mod rewrite:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /countries/hotel.php?hotel_id=$4&state=$1&city=$2 [L,QSA]

Related

.htaccess URL rewrite ignored

.htaccess newbie here.
I have a URL like this:
example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6
that I need to be rewritten like this:
example.com/lesson-plans/earth-sciences/some-title-6
I am using the following .htaccess URL rewrite:
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/([^/]*)/([^/]*)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
However, when I hover over/click on links of the original format (example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6), they are not being rewritten. I've tried a few different rewrite rules, but nothing works.
How can I make this rewrite work? As far as I know, .htaccess is working on my server and rewrites are permitted.
You were close
RewriteEngine on
RewriteCond %{QUERY_STRING} title=([^&]+)&id=([^&]+)
RewriteRule ^lesson-plans/earth-sciences/show_lesson_plan.php$ /lesson-plans/earth-sciences/%1-%2 [QSA,L,R]
Randomly while lying in bed last night.
You have the rewrite rule back to front. you have to add the rule for the rewritten url to turn it back into an ugly one
see: http://martinmelin.se/rewrite-rule-tester/
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/(.*)-(.+)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
so
lesson-plans/earth-sciences/some-title-6
becomes
/lesson-plans/earth-sciences/show_lesson_plan.php?title=some-title&id=6&cat=3
I ended up using the following code and am posting it as an answer in the event someone else finds this useful:
RewriteEngine on
RewriteRule ^([^/.]+)/([^/.]+)$
show_lesson_plan.php?title=$1&id=$2
This will take a url (like this: http://example.com/lesson-plans/earth-sciences/a-cloud-in-a-bottle-i/8923) and run it through this: http://example.com/lesson-plans/show_lesson_plan.php?title=$1&id=$2
Note that I changed the original URL slightly, opting to break the id out of the title string.

simple htaccess RewriteRule to map 404 URL

I have this URL:
mysite.com/index.php?pageType=404
and then I want it to get mapped to:
mysite.com/404
I have written the following rewrite rule but it does not work, what is the problem?
RewriteEngine on
RewriteRule mysite.com/404 mysite.com/index.php?pageType=404
IndexIgnore *
Thanks in advance
RewriteRule's first argument is a regex that matches everything behind the domain name and before the query string. For an url like http://example.com/my/site/is/awesome?give=mecookies, it would match my/site/is/awesome (without the first slash).
The correct rule would be:
RewriteRule ^404$ index.php?pageType=404 [L]
Please read the documentation for more information about this.

URL rewrite to ignore php $_GET statement

I have a url like:
http://www.site.com/profile.php?id=$id
And I want it to end up like:
http://www.site.com/$id
How could I do this in PHP or in htaccess?
You would need to create a rewrite rule in htaccess such as
RewriteRule ([0-9]+) profile.php?id=$1 [L]
This would rewrite anything with a 0-9 character to profile.php and pass the number in the query string. So you could do
site.com/1
Which would be
site.com/profile.php?id=1
N number of discussion gone already here. Anyway, here it is. :)
Your htacces rewrite rule,
RewriteEngine on
RewriteBase /
RewriteRule ^([0-9]+)$ profile.php?id=$1 [L]
Way to link in your PHP files,
Your URL
Make sure to replace $id with your values.
FYI, http://www.site.com/profile.php?id=$id will also works, avoid using it & use mask URL like i quoted above. :)

How to implement URL rewriting two rules with the same extension?

My htaccess code:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)\.html$ post-details.php?post_id=$2 [L]
RewriteRule ^post-year-([0-9]{4})-([0-9]+)\.html$ post-details-year.php?post_year=$1&post_month=$2 [L]
The first rule is working and giving me the correct output but the second one is not; it jumped to the first rule with "post_details.php" desired "Post-details-year.php".
Additionally when I change the file extension:
Example:
RewriteRule ^post-year-([0-9]{4})-([0-9]+)\.html$ post-details-year.php?post_year=$1&post_month=$2 [L]
to
RewriteRule ^post-year-([0-9]{4})-([0-9]+)\.cgi$ post-details-year.php?post_year=$1&post_month=$2 [L]
the rewriting engine starts going over the your rules one by one until it find one that fits.
The problem was that your first rule was very powerful, and it catched URLs you didn't want it to.
When you try and access post-year-123.html, the first rule matches post-year to ([a-zA-Z0-9_-]+) part of the regex, and 123 to ([0-9]+), which then just redirects you according to the first rule.
If you switch them, and put the second rule first, it will first try to match it and will success, thus sending you to the place you want.

htaccess not reading php file

I seem to be lost in a big pile of code. I have the following piece of .htaccess code:
RewriteRule ^user/(.*)$ index.php?p=user/$1 [L]
RewriteRule ^user/(.*)/(.*)$ index.php?p=user/$1&id=$2 [L]
Now, I have a file called profile.php, inside that I use $_GET to get the ID. But when I go to /user/profile/1, it does nothing. When I go there without a ID, my script works. Can somebody help me out and tell me what I am doing wrong?
Thank you. Sorry for the confusing message.
Try these 2 rules:
RewriteRule ^user/([^/]+)/([^/]+)/?$ index.php?p=user/$1&id=$2 [L,QSA]
RewriteRule ^user/([^/]+)/?$ index.php?p=user/$1 [L,QSA]
Explanation:
Reason why your rule aren't working because your regex is wrong. You first rule ^user/(.*)$ has .* which is matching everything after /user/ hence your 2nd rule never fires and $_GET['id'] is always empty.
By changing that to [^/]+ my rule is matching only until next / is found hence both rules co-exist fine.
QSA is just nice flag to have here to preserve any existing query string.

Categories