URL rewrite to ignore php $_GET statement - php

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. :)

Related

adding a question mark to the url in url rewrite in .htaccess

Hi everybody i am trying to append a question mark at the end of my file name in url rewrite, after which will be my url parameters, like example the expected url is like:
http://localhost/project/pass/?something#gmail.com-3861
and the rule that i am using is below:
RewriteRule ^pass/?(\w.+)-(\w.+)$ view/pass.php?useremail=$1&actcode=$2 [L]
but the ? is not working as i assuming it to be a reserved keyword. If i use some other things like - then they work
Can anyone tell me solution to that so that i can use ? in my url rewrite?
RewriteRule does not accept query. Use RewriteCond
RewriteEngine on
RewriteCond %{QUERY_STRING} (\w.+)-(\w.+)
RewriteRule ^pass/$ view/pass.php?useremail=%1&actcode=%2 [L]
If you want to use reserved characters without theirs function (? stands for "zero times or once"), add backslash \ before them.

.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.

Rewriting url having more than one url parameter

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]

SEO friendly URL not working using .htaccess

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below
RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1
The actaul URL looks like this
http://localhost/n/article.php?title=this-is-the-first-news-article
but I want it to look like this :
http://localohst/n/article/this-is-the-first-news-article
When I applied the RewiteRule above it does not change to the desired URL
This should do it. You are missing the n. Not sure why you need the word title though.
RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301]. Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]
If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:
RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
it will be RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

How to escape “?” using regex in .htaccess for mod_rewrite

What is wrong in this rule?
RewriteRule ^page\?v=([^/]+)$ page.php?v=$1 [L,NC]
I just want to make the URL looks like that
http://www.domainname.com/page?sk=info
You don't have to include the query parts if they're not changing anyway.
RewriteRule ^page$ page.php [L,NC]
The RewriteRule will not match any part of the query string. page?v=123 will still become page.php?v=123
Also, your RewriteRule uses ?v= while you talk about ?sk=info
Also, you can find additional information about this mod_rewrite case here:
http://wiki.apache.org/httpd/RewriteFlags/QSA
And another SO entry about this issue here.

Categories