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
Related
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.
I have been smashing my head against wall for whole day but could not understand how can I make my URL show the page title in URL instead of all query parameters.
I just simply wants to convert my this URL
http://www.def.com/post.php?id=1
to
http://www.def.com/my-page-title.html
I read many articles and specially questions on SO but non of them were really answering this. I do not have enough reputations to put other similar links here.
I just want to get the title of my page and show it in my URL.
A fairly standard .htaccess that will route all paths where a directory or file are not found at the path:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^route-page\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /route-page.php [L]
</IfModule>
Thereafter, you have to have the /route-page.php (whatever you name it) read the URI: $_SERVER['REQUEST_URI'], parse "/my-page-title.html" and figure out how to load the correct page from there.
route-page.php
<?php
$path = $_SERVER['REQUEST_URI'];
//query for row of data with that path...
//output results
Because your actual page name may have symbols in it, it's best if you have the expected path "my-page-title.html" in an indexed column of your database. This way, you can quickly grab the page based on the path. Also, since all unfound URLs are going to this page, you need to handle 404 errors manually (i.e. if you don't find a page that matches the path specified, output a 404 error):
if( $pageNotFound ) {
header("HTTP/1.0 404 Not Found");
echo("<h1>Page Not Found</h1>");
}
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. :)
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.
I'm developping a website using php and the "template.inc" class.
The problem is that I want to create a mini-cms that allows the admin to create an "html" page with these mysql attributes:
Table Name : Page
-----------------
id :auto-icremented)
name :varchar
In the architecture, if he created the page number "5", the url is
"ww.mywebsite.com/index.php?id=5".
But, this isn't very esthectic so, as I'm very bad at url rewriting even if i read many tutorials, i want to type the name+"html" to access to the page.
If we take the example of the
"www.mywebsite.com/index.php?id=5"
if the admin created a page with the following values:
id : 5
name : 'home'
i want that the user can type
"www.mywebsite.com/home.html"
and with no redirection as i want that this last url must still appear and become the official url.
Thanks for your answer,
i know how to rewrite www.mywebsite.com/index.php?id=5 to www.mywebsite.com/5.html ... but the problem is that i want, first, to get the "name" vale before and in my example, the name value is "home" (5 =>'home').
How can i access to my database with the url rewriting engine?
Thank you very much,
regards.
Use the .htaccess file from a standard Wordpress install to redirect everything to one PHP file. Something like this...
<IfModule mod_rewrite.c>
RewriteEngine On
# Base is the URL path of the home directory
RewriteBase /
RewriteRule ^$ /index.php [L]
# Skip real files and directories
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
</IfModule>
Then use $_SERVER['REQUEST_URI'] to figure out what the user was looking for. Modify the .* if you want to make the rule more specific, like .*\.html.
Zakaria,
After using Renesis's rewrite rule, $_SERVER['REQUEST_URI'] will be equal to 'home.html'.
Try something like:
<?php
// clean
$page = mysql_real_escape_string($_SERVER['REQUEST_URI']);
// make query
$query = sprintf("select Page.* from Page where name = '%s'", $page);
// find page ID
if($result = mysql_query($query)){
$page = mysql_fetch_assoc($result);
echo "<pre>", print_r($page, true), "</pre>";
}
?>
Possible output
Array (
[id] => 5
[name] => 'home.html'
)
Use Apache URL Rewriting for this. there are many many examples on this site alone of this. You could also try the official Apache rewriting docs.
You will need to make sure your database enforces uniqueness on name, or you will have problems.
Edit
Have your index.php take a name= parameter instead of a id parameter. You will need to make sure your db has the name field indexed so you don't do a table scan for every page request.