Changing URL with query string - php

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.

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

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.

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