htaccess Friendly Urls Rewrite - php

I sincerely apologize if this is a duplicate. I've looked at so many threads but none seem to apply specifically to what I'm trying to do.. and for some reason, I just have such a difficult time wrapping my brain around .htaccess rewrite rules!
I purchased a php script and after installing it, I create a new post and it works fine.. except of course, it shows: mydomain.com/post.php?id=7
Want I want is to show the title of the post after the .com/
I know some will advise to use a post id or a date or something, but I'd really prefer it to be written as stated. So the end result would be: mydomain.com/this-is-my-first-post
I then want to be able to correctly call this page. I assume this would all be done in the index.php page (which calls the post.php page)?
I found the line in index.php that currently calls the post and it looks like this:
<h1><?php echo $theArticle['title']; ?></h1>
Is that also what I would need to update?
Thanks for any assistance. I really have tried but I am just not programming minded and struggling mightily!

Your question is a bit too broad, but if you are willing to compromise, you could change the url to something like:
mydomain.com/post/7/the-title-of-post-7
And then you would only need a rewrite rule and no changes in the database retrieving code.
In that case the rewrite rule would look something like:
RewriteRule ^post/(\d+).*$ /post.php?id=$1
^^^^^ capture the number to be used
You would of course also need to rewrite all links to the required format.

Related

How to make a url virtual 'directory' based on unique Ids

Lets start with an example: I want to do what Tumblr is doing. More specifically, every time you click on the 'reblog' button, the URL changes to something like "/reblog/UNIQUEID/UNIQUEID/".
Yet obviously they don't have millions of directories with html files in each.
So I was wondering, if I want to make a something similar how would I approach it?
We can get the Unique ID via PHP GET and then using JavaScript change the URL to display it as "/dir/uniqueId" instead of "/dir?=uniqueId" but that seems very cumbersome.
And if we do that, it also posses a new question: what if a user enters "/dir/uniqueId" in the URL bar... that wouldn't work because its an edited string from JavaScript.
I'm not familiar with htaccess, yet I'm pretty sure it has a huge implication on the answers.
So, how would one go about fixing this problem of terrible url syntax (?= etc) to something that looks more like directories, but fundamentally isn't?
Recap:
How does one make /dir/uniqueId work while using PHP/htaccess but the uniqueId are not directories or actual files
Here's an example...
.htaccess
RewriteEngine on
RewriteRule ^reblog/([^/\.]+)/?$ index.php?reblog=$1 [L]
index.php
if (isset($_GET['reblog'])) {
$uniqueId = $_GET['reblog'];
//Do some stuff with that $uniqueId
}
url example
http://www.somesite.com/reblog/foo123bar
You can build from this example to match your requirements. As your question stands now, it's too broad to provide a more specific answer.

universal "/success" that shows message if added

I have a problem and I don't know if it's possible but I'm going to ask here.
I have rewrite rules like this:
RewriteRule ^login$ index.php?page=login [NC]
and I want to do something like this, if you add "/success" then the url will look something like this:
"mydomain.com/login/success"
and that will add a $_GET variable to the url and the original url will be:
"index.php?page=login&status=success".
I want to be able to add /success to every page that I have, example: mydomain.com/register/success and so on..
Thanks!
Allright, first thing:
This is not how URI Rewrite works. Sorry man but you really need to study a bit more on the topic.
To solve your issue you need to:
Get the URI using PHP Global Vars
Process it and extract your parameters
There is no way .htaccess will be able to do what you are looking for. I don't usually post my stuff, but i suggest you to read some of the source code of a project i've been working on a few months to understand how routing works. URLParser Example here
Unfortunately to properly understand my code you have a minimal notion of MVC. Hope it helps!

Dynamically changing urls

How do I go about changing the urls on my server to be like
root/category/product/
Rather than how my site currently is
root/index.php?page=item&id=xx
I understand it is to do with the .htaccess file and url rewrites but I have come up with
RewriteRule ^item&id=([0-9]+)$ index.php/product_name=$1
Which doesn't seem to make any difference to the urls at all
Any other problem I think I will have is that the url doesn't display the product name in it at all, so that is something that I will want to include too
I got the idea of your question,but didn't understand the structure you want to follow. Let me give you an example, may be you can get idea from it and solve your problem.
Lets suppose we have a filename and query string as detail.php?item=computer, and we want it to show as "detail/computer".
RewriteRule ^detail/(.*) detail.php?item=$1

Changing URL with query string

I have been looking and looking around on the web for an answer for my question. But everything is just not the right thing.
So my issue is:
I'm creating my own CMS, and right now I've got the issue with the urls. They aren't really that SEO friendly.
When I create a new page, it gets the URL: index.php?page=(id). That doesn't tell much. So what I would love to create.
Is that I wan't the URL to be something like: www.myurl.com/home instead of the page=id. Is this possible?
I have to mention, that I need the id number later on, for editing the pages. I'm focusing the GET function to be able to edit my pages, and to show 'em one by one.
Thanks. :o)
Try to set your .htaccess file to the following:
RewriteEngine On
RewriteRule ^([^/]*).html$ index.php?page=$1 [L,NS]
this way you can translate what visitors see as yourdomain.com/home.html to what php reads as yourdomain.com/index.php?page=home afterwards you can of course use a translating array containing your id's
$translationArray("home"=>1, "contact"=>2);
$id = $translationArray[ $_GET['page'] ]; // $id now contains 1
What you're looking for is called Semantic URLs. Other keywords that will aid you: .htaccess, mod_rewrite
A full solution is too complicated to expand upon here but the underlying idea is fairly simple.

Parsing URLs using PHP

I have posted a similar question here. However, this was more about getting advice on what to do. Now that I know what to do, I am looking for a little help on how to do it!
Basically I have a website that is pretty much 100% dynamic. All the website links are generated using PHP and all the pages are made up of php includes/code. I am trying to improve the SEO of the site by improving the URLs (as stated in the other question) and I am struggling a little.
I am using mod_rewrite of rewriting the nice urls to the ugly urls on the server. So what I need is to now convert the ugly urls (which are generated from the php code in the pages) to the nicer urls.
Here are the URLs I need to parse (these are in the other question aswell):
/index.php?m=ModuleType
/index.php?m=ModuleType&categoryID=id
/index.php?m=ModuleType&categoryID=id&productID=id
/index.php?page=PageType
/index.php?page=PageType&detail=yes
Here is what I want the above URLs to be parsed to:
/ModuleType
/ModuleType/CategoryName
/ModuleType/CategoryName/ProductName
/PageType
/PageType/Detail
There is an example on the other question posted by Gumbo however I felt it was a bit messy and unclear on exactly what it was doing.
Could someone help me solve this problem?
Thanks in advance.
I think I see what you're after... You've done all the URL rewriting, but all the links between your pages are using the old URL syntax.
The only way I can see around this is to do some kind of regex search and replace on the links so they use the new syntax. This will be a bit more complicated if all the links are dynamically generated, but hopefully there won't be too much of this to do.
Without seeing how your links are generated at the moment, it's difficult to say how to change the code. I imagine it works something like this though:
<?php echo "<a href='/index.php?m=$ModuleType&categoryID=$id'>"; ?>
So you'd change this to:
<?php echo "<a href='$ModuleType/$id'>"; ?>
Sorry if I've made errors in the syntax, just off the top of my head...
Unless I misunderstood your question, you don't parse the "ugly" URLs, your PHP script is called with them, so you $_GET[] your parameters (m, categoryID, productID) and you combine them to make your nice URLs, which shouldn't be too hard (just a bit of logic to see if one parameter is there and concatenate the strings).
You will need a front controller, which will dispatch the URL to the correct page.
Apache will rewrite the URL using rules in .htaccess, so that anything written will be redirected to index.php?q=. For example, typing http://example.com/i/am/here will result in a call to index.php?q=/i/am/here
Index.php will parse the path from $_GET["q"] and decide what to do. For example, it may include a page, or go to the database, look the path up, get the appropriate content and print it out
If you want a working example of a .htaccess which will do exactly that (redirect to index.php with ?q=path) take a look at how drupal does it:
http://cvs.drupal.org/viewvc.py/drupal/drupal/.htaccess?revision=1.104
As Palantir wrote this is done using mod_rewrite and .htaccess. To get the correct rewrite conditions into your .htaccess you might want to take a look at a Mod Rewrite Generator (e.g. http://www.generateit.net/mod-rewrite/). Makes it a lot easier.

Categories