Create SEO-friendly URL with PHP with just the Querystring Value - php

Is there a way use mod_rewrite to produce the result below?
Original URL:
http://www.domain.com/shop.php?id=newyork
to
SEO friendly URL
http://www.domain.com/newyork
I've seen plenty of example where the above URL can be converted to http://www.domain.com/shop/newyork but I actually don't want to display the word 'shop/' so just http://www.domain.com/newyork

I'd have a go with something like the following, off the top of my head
RewriteRule ^([a-zA-Z]*)$ www.example.com/shop.php?id=$1
Do bear in mind that anything after your root domain, will be piped into your shop.php script.

Yes, in your .htaccess file put
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/([^/.]+)$ shop.php?id=$1 [L]
</IfModule>
([^/.]+) will match anything that isn't a / or . and store that info,
$1 at the end outputs that info into your shop script.
[L] tells mod_rewrite to stop looking for rules if this one works.

Based on your example:
RewriteRule ^([a-z]+)$ /shop.php?id=$1 [L]
Would match newyork, alaska, hamburg but not highway-1.

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.

how to rewrite html url as directory in htaccess

I want to rewrite all of my category urls as directories instead of long html pages. How do I do that? (The site is homewetbar.com if you need to look at the directory structure further to understand how it currently works.)
For Example:
First level category:
www.site.com/great-gift-ideas-c-35.html
I would like to display instead as:
www.site.com/great-gift-ideas-c-35
Second level category:
www.site.com/great-gift-ideas-gifts-recipient-c-35_85.html
I would like to display instead as:
www.site.com/great-gift-ideas/gifts-recipient-c-35_85
Third level category:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43.html
I would like to display instead as:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43
OR:
www.site.com/great-gift-ideas/gifts-recipient/groomsmen-gifts-c-35_85_43 (whichever is easier)
I think mod_rewrite is what you are looking for, here is a link with how to set it up and how to start rewriting your urls. It should have enough information for what you are looking for, for more advanced stuff you will want to know more about regular expressions, but your examples are simple enough.
RewriteEngine on
RewriteRule ^great-gift-ideas-c-35.html$ great-gift-ideas-c-35
this should work for your first example, the rest just replace out the appropriate urls.
You would need to rewrite all the href in your site to delete the ".html" part. Then, add this to your .htaccess
RewriteEngine on
RewriteCond ${REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [L]
That would make any URL without the ".html" work as if it had it
Just check out this site: http://www.generateit.net/mod-rewrite/index.php it generate all you need
RewriteEngine On
RewriteRule ^([^/]*)$ /?example=$1 [L]
Changing example to what you need should do it

How to Convert Query String to Segment URI?

I'm trying to convert a query string;
http://atwd/books/course?course_id=CC100&format=XML&submit=Submit
Into a segment URI;
http://atwd/books/course/CC100/XML
I'm working in CodeIgniter.
I was looking at a stackoverflow answer that said to check CodeIgniter's URL segment guide, but I don't think there's any information on how to convert a query string into a segment URI. There is, however a way to convert a segment URI into a query string, which is bringing up a load of results from Google too.
Following another stackoverflow answer, I tried this in my .htaccess file but nothing seemed to work
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
In my entire .htaccess file I have this;
<IfModule mod_rewrite.c>
RewriteEngine on
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
#Source: https://stackoverflow.com/questions/3420204/htaccess-get-url-to-uri-segments
#Format Course function requests
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^$ /course/%1/format/%2 [R,L]
</IfModule>
This is in my root directory of Codeigniter screenshot
My code in the .htaccess file isn't working, I refresh the page and nothing happens. The code to hide the index.php is working though. Does anyone know why?
The notion of "converting URLs" from one thing to another is completely ambiguous, see the top part of this answer for an explanation of what happens to URLs when redirecting or rewriting: https://stackoverflow.com/a/11711948/851273
There's 2 things that happen, and I'm going to take a wild stab and guess that you want the 2nd thing, since you're complaining that refreshing the page doesn't do anything.
When you type http://atwd/books/course?course_id=CC100&format=XML&submit=Submit into your browser, this is the request URI that gets sent through mod_rewrite: /books/course. In your rule, you are matching against a blank URI: RewriteRule ^$ /course/%1/format/%2 [R,L]. That's the first reason your rule doesn't work. The second reason why it doesn't work is because above that, everything except images and index.php and robots.txt is being routed through index.php. So even if you were matching against the right URI, it gets routed before your rule even gets to do anything.
You need to correct the pattern in your rule to match the URI that you expect to redirect, and you need to place this rule before the routing rule that you have. So everything should look roughly like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} ^course_id\=([^&]+)\&format\=([^&]+)$
RewriteRule ^/?books/course$ /course/%1/format/%2 [R,L]
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
</IfModule>
You'll need to tweak the paths to make sure they match what you are actually looking for.
To both redirect the browser and internally rewrite back to your original URL, you need to do something different.
First, you need to make sure all of your links look like this: /course/CC100/format/XML. Change your CMS or static HTML so all the links show up that way.
Then, you need to change the rules around (all before your codeigniter routing rule) to be something liek this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# redirect browser to a URI without the query string
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /books/course/?\?course_id=([^&]+)&format=([^&]+)
RewriteRule ^/?books/course$ /course/%2/format/%3? [R,L]
# internally rewrite query string-less request back to one with query strings
RewriteRule ^/?course/([^/]+)/format/([^/]+)$ /books/course?course_id=$1&format=$2&submit=Submit [L]
#Source: http://codeigniter.com/user_guide/general/urls.html
#Removal of index.php
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php?route/$1 [L]
</IfModule>
I'm not going to address the misunderstanding already addressed pretty well in the other answer and comments, and I can't speak for CodeIgniter specifically, but having given their URL routing docs a quick skim, it seems pretty similar to most web frameworks:
You probably just want to direct all traffic (that doesn't match physical files) to the frontend web controller (index.php) and handle the URL management in CodeIgniter's routing, not a htaccess file.
To do that, your htaccess could be as simple as:
<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php [QSA,L]
</IfModule>
This, as I said, will redirect any traffic that doesn't match an physical file such as robots.txt or an image to your index.php.
Then, using the routing as described in the docs (http://ellislab.com/codeigniter/user-guide/general/routing.html) you can take in parameters and pass them to your controllers as you see fit, there is no need to 'convert' or 'map' anything, your URL's don't need to resolve to /?yada=yada internally, based on your routing rules CodeIgniter can work it out.
You'll need wildcard routes such as this from the docs:
$route['product/:id'] = "catalog/product_lookup";
A rough example of what yours might end up looking like would be something like:
$route['course/:id/format/:format'] = "course/something_or_other_action";
If I'm understanding you correctly, you might be over-thinking it. I have something similar in my own code.
I have a controller named Source. In that controller, I have the following method:
public function edit($source_id, $year)
{
# Code relevant to this method here
}
This produces: http://localhost/source/edit/12/2013, where 12 refers to $source_id and 2013 refers to $year. Each parameter that you add is automatically translated into its own URI segment. It required no .htaccess trickery or custom routes either.

.htaccess url rewrite for single page website

I have a single page website that changes content based on variables passed through the URL with PHP.
For instance, my url displays as
www.mysite.com/index.php?section=home
www.mysite.com/index.php?section=about-us
so forth and so on, depending upon which link you click in the main navigation.
I want the urls to read as www.mysite.com/home or www.mysite.com/about-us.
My mod-rewrite feature is enabled because before I made this a one page site, it was functioning correctly.
I've tried this...
RewriteEngine on
RewriteBase /
RewriteRule ^home/?$ index.php?section=$1 [NC,L]
I've tried every suggestion I found on Google and on StackOverflow, nothing is working.
Any help would be appreciated!
I have an .htaccess for exactly the same purpose, and it's working beautifully. My code is below.
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?id=$1 [L]
Naturally, if index.php isn't your main PHP file, replace that with what is. Also, replace ?id with ?section if your example code is what your using.
What the [^/\.]+ means is saying I need you to find text that contains anything but a period (.) or a forward slash (/), and there has to be at least one character in it.
For an example, go to the website I'm using it on, Northside Aikido.
If you have any questions, please feel free to ask.
EDIT
Note, this code won't turn http://www.example.com/index.php?section=home into http://www.example.com/home automagically, it'll just mean that the latter link works like the former. If you want it to automatically replace it, you'll need more code than one line.
this is what im using:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?section=$1 [NC,L]
</IfModule>
Almost
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?section=$1 [NC,L]

URL rewrite to remove parameters

I'm working on a site where all the pages are actually index.php + a 'name' parameter that is analyzed
and loads the appropriate template and content.
the homepage url is:
http://www.some_site.com/?page=homepage
1. i was asked to "change" the homepage url to:
http://www.some_site.com
can i use url rewite and htaccess for that and if so, what should i write there?
working on my local machine, i tried this code (mode rewrite is enabled):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule /index.php /index.php?page=homepage
</IfModule>
i would still need the 'name' parameter to be available to the php code of course, so i can load the
template and css files.
2. it would be nice for other pages (not homepage) to be converted from (example)
http://www.some_site.com/?page=products
to:
http://www.some_site.com/products
this is less crucial.
thanx in advance and have a nice day :-)
You don't need a rewrite rule at all. Just change your index.php file to show the homepage when there is no page variable at all.
if (!isset($_GET['page'])) {
$_GET['page'] = 'homepage';
}
For educational purposes, the rewrite rule:
RewriteRule /$ index.php?page=homepage [L]
That is, the URI to match is just the slash (the URI starts after your domain in the URL). The $ means that there should be no characters after the slash.
As for products and such, assuming single words made of only letters:
RewriteRule /([a-zA-Z]+)$ index.php?page=$1 [L]
The following should be what you're looking for (for your second, less crucial, question). Put it in your .htaccess-file:
RewriteEngine On
RewriteRule ^/([a-zA-z0-9-_]+)/?$ index.php?page=$1

Categories