.htaccess read from MySQL - php

I have a simple .htaccess file for vanity URLs, like this:
RewriteEngine On
RewriteRule ^example-category/?$ category.php?id=1 [NC]
The idea being that all category URLs instead of looking messy redirect to plain English versions.
My categories are stored in a MySQL database. So I'm wondering if it's possible (and advisable) to have the .htaccess file read directly from the database. This way if I add or delete a category the vanity URLs will update accordingly without manual intervention.
Is this a good idea? And if so how can you get a .htaccess file to read from MySQL?
Thanks

You can use the category name instead rather than using the ID. Here is a quick example.
htaccess file.
RewriteEngine On
RewriteCond %{REQUEST_FILEMAME} !-f
RewriteCond %{REQUEST_FILEMAME} !-d
RewriteRule ^([^/]+)/?$ /category.php?name=$1 [NC]
This will take a URL like
http://example.com/books/ and route it to your php file.
Then in your php file you can get it with something like this to use the name.
<?php
$cat = $_GET["name"];
$query = "SELECT * FROM categories WHERE name='".$cat."'";
?>
That's not a complete PHP example. Just showing you how you can us the name instead of the ID. Note: I didn't take in consideration for security via mysql etc.

Related

Unable to append query to url ending like /?user

www.something.com/?user, where "user" is a php file, is there any way to add query string parameters to it?
Have failed with:
www.something.com/?user?id=1
www.something.com/?user&id=1
www.something.com/?user/id=1
Have seen this topic: Get URL query string
but this is not exactly what I'm after...thanks
The way to do it with the php extension intact would be:
www.something.com/user.php?id=1
You then use:
$userID = $_GET['id'];
If you want to drop the PHP extension, you can do this with a rewrite in the htaccess: This is pretty generic as it depends on your web-server, but something like:
RewriteRule ^www\.something\.com/user\.php$ /www.something.com/user?&%{QUERY_STRING}
Or you could just drop using the PHP extension on all of the files to keep it neat using something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
This will rewrite all of your php extensions so this:
something.com/someFileName
Will automagically be read as
something.com/someFileName.php
To which you can still use:
something.com/someFileName?someQueryParameter=someValue

PHP : url rewriting

I want to know how to page url without .php extension
for ex here is my website :
http://mywebsite.com/ now from the home page whenever i click on any gallery it will goes to the page gallery.php with querystring of galleryID for ex
http://mywebsite.com/gallery.php?id=29
So instead of this gallery.php?id=29 I want to make the url something related to the page title
http://mywebsite.com/9-WEDDING-GIFT-IDEAS
Thanks in advance
I'd recommend using a PHP framework once you start going down this path, that gives your application/site a structure, and the ability to setup routes (paths) like you're requesting.
I'm a fan of Laravel, this allows you to use http://example.com/index.php/friendly-url/goes-here if you haven't setup Apache (the web server) to remove them.
You can remove the index.php/ part and just have http://example.com/friendly-url/goes-here by using the Apache mod_rewrite which Laravel includes for you. Check out the documentation under Pretty URLs
Hope that helps.
There are two ways you can do this. If you don't have a ton of URLs, you can add them in to your '.htaccess' file manually.
RewriteEngine On
# MANUAL
RewriteRule ^9-WEDDING-GIFT-IDEAS/?$ gallery.php?id=29 [L]
RewriteRule ^MOST-BEAUTIFUL-WEDDING-LOCATIONS/?$ gallery.php?id=30 [L]
# ...
Otherwise, you can have 'gallery.php' handle looking up which article to display based on the title. So if it receives a title of '9-WEDDING-GIFT-IDEAS', then it can look up that title in the database and fetch the article for that title. Here, the article title will be passed as the 'id' parameter to 'gallery.php'.
RewriteEngine On
# PARSING HANDLED IN gallery.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ gallery.php?id=$1 [L]
EDIT:
<?php
// gallery.php
$article_title = $_GET['id'];
// ...
// DO A DB LOOKUP TO GET THE ARTICLE ID WHERE TITLE = '$article_title'
// OR JUST GET THE ARTICLE BASED ON THE TITLE INSTEAD

Use .htaccess to rewrite post.php?post_id=20 to /post/this-is-the-title/

I am new to .htacces and for ages I have been trying to get my domain from:
www.domain.com/post.php?post_id=20
to something such as this:
www.domain.com/post/this-is-the-title/
As I said I am new to .htaccess but anything would help please!
NOTE: I would be getting the titles of my blog posts from an SQL database somehow
If the page titles are database-driven (or otherwise dynamic), I don't see how you could get away with .htaccess rewrites. It would make more sense to use routing in your PHP script.
I have written about an extremely simple routing method here. It's for people only getting into the subject, but you should be able to build a database-driven router based on that.
Essentially, route all your traffic through index.php and there, get the request URI and decide what resources to load.
[EDIT]
Let me elaborate a bit, using the info from my blog post.
Assuming you have your site set up and running, first direct the traffic to index.php. You do that in .htaccess and it can be done like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Then, in index.php (or in a script that's called from index.php), you can get the request URI:
$uri = $_SERVER["REQUEST_URI"];
OK, so far so good. Now let's assume your database holds page content along with the page aliases. The uri will most probably be the page alias, so all you need to do is something like this:
$pdo = new PDO(/* your db connection params */);
$page = $pdo
->query("SELECT * FROM pages WHERE alias = :alias",
array("alias" => $uri)
)
->fetch();
At this point you should have the page content corresponding to the title (or alias) in the URI string. All you need to do now is display it any way you want:
echo $page["content"];
You can't. You have to put the title on the query string.
An .htaccess cannot safely get anything from the database.
Since you are going to rewrite the url, why not simply write the post title in the URI?
You can't do this with an .htaccess cause you need to get "the titles of your blog posts from an SQL database somehow", do a redirect with PHP. :)

Custom link for each user in PHP

The standard GET query as a link for each user is easy but ugly for the users and search engines.
// This looks UGLY
website.com/index.php?userid=43232
//This is what I want
website.com/coolUser
How to make a custom link for every user without creating folders all the time?
It's called "routing". You may begin with this article: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/
What you mentioned called "clean URL", and what you need something known as "mod-rewrite" capability in Apache configuration.
Sergei and Zulkhaery direct you to it with their comments. Also you can take a look at the link below:
http://wettone.com/code/clean-urls
But don't forget you may see some problems with you css, js, and image files. But they have their own solutions too. You can find them if you get these problems.
Simple way to make URL like that, using .htaccess (create .htaccess file and upload to your public_html / wwwdir)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z-]+)/?$ index.php?userid=$1 [L]
Now, you can access http://yoursite.com/43232 OR http://yoursite.com/index.php?userid=43232

Pretty URLs from DB

I am working on creating page links from DB like the following example.
Current page:
www.example.com/page.php?pid=7
In the DB it is saved as title "contact-us" under category "Company Info"
I want it to be like:
www.example.com/company-info/contact-us.html
I have tried different solution and answers but did not got any luck. I am not sure, where will be the PHP part and which rules to write for .htaccess files.
In apache (or .hataccess) do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /proxy.php?_url=$1 [QSA,L]
So in a nutshell, if the resource being requested doens't exist, redirect it to a proxy.php file. From there $_REQUEST['_url'] will be the url the user was requesting.
Then create proxy.php in your home directory and add whatever logic you'd like to load the correct content.
If you use this from .htaccess, then you may need to add RewriteBase / to your config.
If you want to find this page by url, you will probably do this through php and .htaccess. Make a .htaccess that calls page.php for each and every request. You don't need the pid=7, because, well, how should the .htaccess know it is 7, right? :)
In page.php, you take the original url and split it on the slashes, so you get the category (company-info) and the page itself (contact-us.html). Then, you can look these up in the database. This is in a nutshell how many software works, including Wikipedia (MediaWiki) and CodeIgnitor.
Mind that 'company-info' isn't the same as 'Company Info'. You'll have to specify the url-version in the database to be able to use it for look-up.

Categories