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

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.

Related

Linking vanity URLs to query string URLs

I'm trying to link non-existent vanity URLs such as:
www.sample.com/albums/artist/title
... to resources that take the form of:
www.sample.com/albums/album.php?id=1
I know this requires some Apache redirect or internal rewrite, but no example I've found seems to work for me. It either: does nothing; or gives an error.
How to you propose Apache knows which album has which ID..?
In short, you cannot link an album name to an ID like that. The information has to come from somewhere. What you can do, however, is to add support for looking up via the name of the album. Then you can use a redirect to something like this:
www.sample.com/albums/album.php?artist=\1&name=\2
Edit, added:
Seeing there is a set and unchanging number of albums, this question becomes a very simple one. What you have is a number of static URLs, which should be mapped to another static URL. Which is the first example listed in the Apache Rewrite Engine manual.
So simply map /albums/artist_1/title_1 to albums/albums.php?id=1, and do this for each album you have.
Not very elegant, but it is the simplest and least error-prone solution.

htaccess Friendly Urls Rewrite

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.

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

Using mod_rewrite to substitute variable used in URL

I've gone through a few different questions like:
Rewrite for all URLs
Can mod_rewrite convert any number of parameters with any names?
Creating dynamic URLs in htaccess
Which helped me change one set of urls from domain.com/script2.php?d=1 to domain.com/(d), but now I'm stuck with something that I can't find an answer for. Currently, I have a set of URLs that are set up as:
domain.com/script.php?a=1
While I know how to change those URLs to domain.com/(a) this doesn't quite help me with this one because variable A is just a numerical identifier, so going from domain.com/script.php?products=1 to domain.com/1 doesn't do me a lot of good.
Instead, it's variable B which is actually the descriptor, ProductName. So what I'm trying to do is have it so that rather than domain.com/(a), I can get domain.com/(b). There is a complication. The reason that the original set up used variable A is that multiple products use the same descriptor in variable B, so I also need to include variable C which differentiates them, so I need the URL to be domain.com/(b)-(c).
Bonus! Remember how I said I had another script that I'd changed from domain.com/script2.php?d=1 to domain.com/(d)? Well, it'd be super awesome if I could set up my this current script to display not as domain.com/(b)-(c) but instead as domain.com/(d)/(b)-(c) because domain/(d) is actually the search page for this other script, so it's a really logical flow and would really simplify browsing, and would let users intuitively move between the search and the products without much work.
I have no idea if this is even possible, but any help would be appreciated.
Why not just rewrite everything back to your script file?
RewriteEngine on
RewriteBase /
RewriteRule .* index.php [L]
Will rewrite everything back to index.php. From there you can parse the $_SERVER['REQUEST_URI'] variable in PHP. From there you can decide what page to load based on the given url.
If you have any other folders in the same directory of the rewrite rule above, you can put another .htaccess file inside those that have RewriteEngine Off if you don't want them to be rewritten back to index.php. That is what you will need for a css file or site images.
Using this method, you could always do something like this.
domain.com/products/1
or, domain.com/search/blahblah

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.

Categories