URL rewriting with PHP MySQL .htaccess & GET - php

I have a MySQL database with 5000 items. The table is structured as
id
product-name
content
product-url
You can access a specific product page by going to www.site.com/products/index.php?id=123. The index.php file uses PHP to GET the ID to search the MySQL database and return the appropriate product name and content.
How can I make it so instead of www.site.com/products/index.php?id=123, the URL is rewritten or redirected to www.site.com/products/product-url? If I do it with .htaccess, will the index.php file still be able to know what ID to use to look up the product-url if the ID is not in the URL? Or is there some way to map URLs with PHP and MySQL?
(I'm a noob when it comes to this stuff, so any help will be much appreciated. Thanks.)

You can use .htaccess and a RewriteRule. The following will redirect an SEO URL to index.php with $_GET['product-url']. You will need to modify your code to either lookup the product id or use the product-url directly. Both should be unique in your products table.
RewriteEngine On
RewriteBase /
# redirect product page from SEO URL
RewriteRule ^products/([\w\d-_]+)/?$ products/index.php?product-url=$1 [L]
Note: I've made the product_url to only allow letters, numbers, dashes and underscores. I suggest such a restriction. In addition, the trailing slash is optional. Futhermore, this will not reappend a query string. If you need that you can add the QSA flag to the RewriteRule.

Related

mod rewrite - have nice urls but still build menu dynamically from database

I am creating my menu dynamically from a database. I started by appending an id to the urls which I used to identify the page in the database and then get all urls from the subpages and so on..
so I had a lot of ids like this www.page.com/somefolder/pagename.php?id=10, where 10 is the id of the page and is the parent_id for a number of other pages.
Now I use mod rewrite to get nicer urls. So the above url is now www.page.com/pagename.php , which is mapped to the original url.
My pagenames are all gonna be unique.
The question: Do I have to do this for each and every url, since each url needs an id as query string or is there a better way to combine mod rewrite with dynamically generating a menu?
No, not for every URL, but you will need to do this the other way around.
Turn the rewrite engine on
RewriteEngine on
Build a path to match against using a dynamic 'case' in brackets..
RewriteRule ^/?somefolder/page/([0-9]+)/?$
.. complete the rule with the rewritten URL, using $1 for the 'case'
/somefolder/pagename.php?id=$1 [L]
Put it all together using [L] to signify the 'last' rule to execute.
RewriteRule ^/?somefolder/page/([0-9]+)/?$ /somefolder/pagename.php?id=$1 [NC,L]
This will rewrite /somefolder/page/12345/ to /somefolder/pagename.php?id=12345

Rewriting php urls to produce clean urls .htaccess

I'm using a custom built CMS and I need to rewrite the urls because currently they take the page ID and name of the page into the url.
/pages/1411760825/Facilities is an example of the current url
/Facilities is what I want it to show up as in the URL bar
The pages are linking to each other based on their ids and titles and I want it to not do that anymore.
This is the current .htaccess rule
RewriteEngine on
RewriteRule (.*)\.xml(.*) $1.php$2 [nocase]
I know I need to add a line below to rewrite what I want it to be, but I'm unsure how to achieve this.

Redirect requested asp urls to php using htaccess

We had a major site rework and now our new site is PHP based. We have some sites which have our old pages linked to them which were asp pages in the past. I want to create a htaccess rule but I have failed to get the desired result. If anyone can help it would be great.
The incoming pages will be in the format
http://www.mysite.com/tour.asp?id=1234
where 1234 is dynamic and can vary. I want to redirect this to the page viewer.php and change the query string variable id to tourid so the page requested should be
http://www.mysite.com/viewer/1234
I have another rule in htaccess which routs the above url to
http://www.mysite.com/viewer.php?tourid=1234 (this is working)
I need a rule by which the 1st url is routed to the 2nd one hoping it will open the 3rd url. How can i achieve this?
Since I am new to htaccess, can someone help me around how to achieve this routing? I want to clear here that the server now is php and I have to accomplish this using htaccess only. I followed other similar questions but found none that answers my query.
UPADTE - I got it wrong first time, here's the corrected version.
# Match and capture the id value from the URL
RewriteCond %{QUERY_STRING} id=([0-9]+)
# Redirect by passing the ID to the new site - the ? at the end removes existing query string
RewriteRule tour.asp http://www.mysite.com/viewer/%1? [R=301,L]
I've assumed the IDs are numeric only.
The source for this answer is at https://httpd.apache.org/docs/2.2/mod/mod_rewrite.html - search for "Modifying the Query String" to find the info about removing the query string.

conditional .htaccess redirect - redirect to two different pages depending on the url

My website functions a little like 9gag or damnlol, etc.
when the user visits an item page, for example: site.com/104-funny-thing, the rewrite rule redirects to view.php?id=104-funny-thing. Here the URL is stripped away of everything after the first '-'. So we are left with the id only, in this case: 104, and the query gets the item.
Here is the Rule:
RewriteRule ^([A-Za-z0-9-]+)$ view.php?id=$1
What I want is that the same URL format could be used to open up user profiles as well. For example: site.com/salman, this URL should open up profile.php?username=salman.
I thought of doing this via PHP. Where I can use the same file, view.php, for both purposes. Once the parsing of the URL is done, if I get a numeric value, I would include the item.php page and query and show data accordingly. If it's not numerical, then I ca include the profile.php page and show the profile instead. This should theoretically work flawlessly, but what I wanted to try was that if there was a way to redirect using the .htaccess directly, I would prefer to use that option, as the previous option requires extra work to sort out some issues that the solution causes.
Someone good at htaccess (regex code?) might be able to offer a solution. Here is some further explanation:
Usernames can contain numbers, but not only numbers
Username URLs cannot contain '-'
Item IDs cannot contain alphabets
URLs 104-funny, 104- and 104, all work for view.php
Thank you for your help in advanced!
#article urls
RewriteRule ^([0-9]+)(-.*)?$ view.php?id=$1
#profile urls.
RewriteRule ^([A-Za-z0-9]+)$ profile.php?username=$1

hide ID mod_rewrite

is there a way to hide IDs using only .htaccess and without changing my php code?
htt://www.domain.com/show.php?categoryID=2&cname=electronic&productid=21&name=laptops
to
htt://www.domain.com/electronics/laptops
thanks in advance.
In your .htaccess you can put:
RewriteEngine On
RewriteRule ^(electronics|other|categories|here)/(laptops|other|products|here)/$ shop.php?cname=$1&name=$2
Basically I made a RegEx that matches all of your products and all of your categories, and broke that out into the cname and name and passed them to show.php.
However note I didn't pass the ID numbers to your show script. Rewrite Engine can't figure those out for you, your script will need to take the names and look up the ids itself.

Categories