URL rewriting with 3 variables - php

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.

Related

htaccess rewrite URL with multiple php parameters

So this is likely a duplicate, but I have exhausted every example on here and nothing works.
I am working on an adult site for someone, and using it as an excuse to learn something along the way.
Take this URL:
http://www.websitename.co.uk/profiles/masseur/profile.php?id=5&name=Erika%20S
I want it to look like this:
http://www.websitename.co.uk/profiles/masseur/Erika_S
So 2 things need to happen:
URL rewrite without being redirected, disregarding the id parameter
spaces in the name being replaced with an underscore (or a dash)
Rewrite is on. Other rewrites work such as removing file extensions (is this interfering? Although it don't work even if I disable this.)
I am losing whatever hair I have left.
please help!
How do you know which id to use? You could possibly use a Rewritemap to look it up, but it depends on how you know what it is (As Heinrich pointed out in the comments).
Also translating the '_' to a space, though possible, would be much easier to do in profile.php that in the rewrite. What's slightly confusing is you say you want to translate spaces to underscores, but in your example you do the opposite (to encoded spaces at least). Could you clarify, or correct my misunderstanding.
Here is a base rule to start you off
RewriteRule ^/profiles/masseur/(\w+) /profiles/masseur/profile.php?id=5&name=$1

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.

URL rewriting - Beginners guide

I need to learn how to do URL rewriting for a site to get pretty URLs...
I have a general idea of how it works through the 50 tutorials I read. But none of them really clicked and never managed to make it properly work.
Do you know of any tutorials that are good for me to start?
Here is an example of how I want to use it:
Here is the basic link
http://www.dico2rue.com/dictionnaire.php?idW=675&word=Resto-basket
I want it to become like this:
http://www.dico2rue.com/dictionnaire/675/Resto-basket
But when I did that, all my links (like for CSS sheets, images,...) didn't work. Do I need to full linking (which apparently slows your site down)?
Thanks for your help.
UDPATE
There are 2 options for the links
PAGE = ?page=1
WORD = ?idW=45&word=google-friendly-url-part-here
The code in htaccess is
RewriteRule ^dictionnaire.php/p([0-9]+)?$ dictionaire.php?page=$1 [NC,L]
RewriteRule ^dictionnaire.php/([0-9]+)/([a-z])?$ dictionaire.php?idW=$1&word=$2 [NC,L]
I hope this helps.
Have a play with something like CodeIgniter, Wordpress, or Drupal. You can see how the URLs are formed and how they map to the contents of .htaccess.
This mod_rewrite cheatsheet is very useful.
Given that your stylesheets are already referenced absolutely (they include http://www.dico2rue.com/) it's probably because you're telling all your images, stylesheets and JavaScript files to go through index.php. Instead you need to tell .htaccess to leave physical files alone, and only parse other URLs.
Post what you have in your .htaccess file here and I'm sure we can help fix it.
Check if you're requesting a existing file using a (negative)RewriteCond using flags -f and/or -d. Used in many tutorials, check the topmost results at any search engine.

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