Dynamically changing urls - php

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

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.

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.

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.

URL rewriting with 3 variables

I want to do URL rewriting of my webpage. There are 2 sorts of links possible on the same page as follows:
Pagination:
http://www.xxxxx.com/dictionnaire.php?page=4
That I want to look like this:
http://www.xxxxx.com/dictionnaire/p4
Word:
http://www.xxxxx.com/dictionnaire.php?idW=675&word=Resto-basket
That I want to look like this:
http://www.xxxxx.com/dictionnaire/675/Resto-basket
In the .htaccess, I have the following:
RewriteRule ^dictionnaire/p([0-9]+)?$ dictionaire.php?page=$1 [NC,L]
RewriteRule ^dictionnaire/([0-9]+)/([a-z])?$ dictionaire.php?idW=$1&word=$2 [NC,L]
QUESTIONS:
Is this the best google friendly way of doing this? (mostly for the word link, or is there better?)
Can you have 2 rewrite rules for one link? Like above?
Is there an error in my code, is so, please help.
When I created this code, my CSs and images weren't appearing. Can you help me fix it?
I know it's a long question, but I thought it would be easier that way.
Thank for the help.
Is this the best google friendly way of doing this? (mostly for the word link, or is there better?) Well, you could put page4 in the Pagination part, either won't really affect it much. For the Word page, why do you have it find words by IDs instead of the actual word? the word=(WORD) doesn't really seem to do anything at all. Perhaps remove ID entirely and have it search by word so that it could be dictionnaire/word/(WORD) instead.
Can you have 2 rewrite rules for one link? Like above? Yes, it is completely possible to have more than 2 rewrite rules (Think many forums that do this)
Is there an error in my code, is so, please help. I haven't looked hard, but it doesn't appear to be any errors.
When I created this code, my CSs and images weren't appearing. Can you help me fix it? The problem here is it is searching /dictionnaire/p4/css.file.css for your css file. It isn't looking in your root directory like I suppose you want. Put the direct root to your CSS file starting with a / at the beginning.
This should be "Google friendly".
Yes, rewrite rules are applied in order; as soon as one matches and replaces the URL, it's unlikely any further ones will match (since they'll be working on the replaced URL of the previous one).
Looks OK to me. You could make it into one rule though, if you allowed the destination URLs to be a little different (to both use page= rather than idW= on the second one).
That's because the browser will ask for resources relative to the non-rewritten URL (it of course doesn't know about what's going on behind the scenes). You'll have to use absolute URLs for your images and CSS (or alternatively, use ../ in the URLs, or add more rewrite rules for your resources to make them work).
Hope that helps.
EDIT: Sorry, because you have that [L] at the end of your rules, it will stop trying to match any more rules once it matches one. This won't have any practical effect in this case.

Categories