Solved

Simple(?) Rewrite rule for PHP website navigation ie "domain.com/artists" instead of "domain.com/index.php?n=3"

Posted on 2006-07-19
6
500 Views
Last Modified: 2013-12-12
I have a ton of websites that are driven by a mysql table for the nav, structured so that every page references the index.php, which includes a content page based on the nav variable passed in.  So if a website has three sections like "artists", "news", and "publications", a nav table holds

nav_id ; nav_name
1 ; arists
2 ; news
3 ; publications

and the URLS look like:

http://www.domain.com/index.php?n=1
http://www.domain.com/index.php?n=2
http://www.domain.com/index.php?n=3

How hard is it to use a rewrite rule so that I don't have to change CODE, and these sites can still work this way, but the the pages could be called up with

http://www.domain.com/artists
http://www.domain.com/news
http://www.domain.com/publications

And the URL STICK on that (ie not meta refresh to index.php?n=1 etc..)?

This has become more important obviously with search engine optimization, but obviously I'm looking for the simplest way to upgrade legacy sites.

 - I hear rewrite rules can be incredibly complicated.  Since my structure is so consistent accross so many sites, can someone give me an idea of how "simple" my situation is?  What's the easiest way for me to learn how to do this - can someone point me in the right direction?  Linux redhat apache cpanel php/mysql environment

 - BTW is this actually that important to search engine optimization or is it just an aesthetic / user friendly thing?

 - I do sometimes have subnavigation, so there are two navigation variables, requiring two directories like /news/archived and news/current or something to that effect..

Thanks for any help - basically I'm very familiar with php/mysql and work at the shell but don't know c++ etc. and wondering if someone can guide me to the best solution and most efficient way for me to learn how to do it, whether it's a recommended book, web tutorial, sample code, etc..








0
Comment
Question by:centerforward
6 Comments
 
LVL 10

Assisted Solution

by:Khanh Doan
Khanh Doan earned 50 total points
ID: 17142942
Try this:
---- .htaccess ----
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([A-Za-z0-9]+)$ index.php?n=$1


Goodluck.
Bonmat86.
0
 
LVL 24

Assisted Solution

by:slyong
slyong earned 100 total points
ID: 17143056
hi,
if you want
http://www.domain.com/artists to display http://www.domain.com/index.php?n=1
http://www.domain.com/news to display http://www.domain.com/index.php?n=2
http://www.domain.com/publications to display http://www.domain.com/index.php?n=3

do this:

RewriteEngine On
RewriteRule ^artists$ index.php?n=1 [L]
RewriteRule ^news$ index.php?n=2 [L]
RewriteRule ^publications$ index.php?n=3 [L]
0
 
LVL 24

Assisted Solution

by:slyong
slyong earned 100 total points
ID: 17143071
- I hear rewrite rules can be incredibly complicated.  Since my structure is so consistent accross so many sites, can someone give me an idea of how "simple" my situation is?  What's the easiest way for me to learn how to do this - can someone point me in the right direction?  Linux redhat apache cpanel php/mysql environment
There are a lot of tutorials about rewrite.. just might interest you http://tools.devshed.com/c/a/Web-Development/Module-mod-rewrite-Tutorial-Part-1/

 - BTW is this actually that important to search engine optimization or is it just an aesthetic / user friendly thing?
It is for both reasons

 - I do sometimes have subnavigation, so there are two navigation variables, requiring two directories like /news/archived and news/current or something to that effect..
You can keep adding the rules.. but I think it is better off for you to change some of your PHP code to take something like index.php?n=news&s=current then you can use regex to write your rewrite rules.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:centerforward
ID: 17157604
ok thanks everyone! -- let me try the code out and i'll split points asap, got a bit behind with work this week
thanks very much!!
0
 

Author Comment

by:centerforward
ID: 17164678
I created the
.htaccess file in the root, with the following contents:

RewriteEngine On
RewriteRule ^home$ index.php?n=1 [L]
RewriteRule ^about$ index.php?n=2 [L]
RewriteRule ^services$ index.php?n=3 [L]
RewriteRule ^contact$ index.php?n=4 [L]

Live at www.mav-computers.com as a test...

The URL "http://www.mav-computers.com/about" is NOT FOUND

Does rewrite need to be turned on in cpanel, or apache refreshed, something clever like that, or is the code even right?

Obvioulsy I'm trying to get "http://www.mav-computers.com/about" to load the same page that "http://www.mav-computers.com/index.php?n=2" loads now, but the address bar in the browser to stick on displaying
"http://www.mav-computers.com/about"

Thanks!


0
 
LVL 20

Accepted Solution

by:
Muhammad Wasif earned 350 total points
ID: 17240757
mod_rewrite is enabled in CPanel.
Try to changing the rule as follows

RewriteEngine On
RewriteRule ^home$ /index.php?n=1 [L]
RewriteRule ^about$ /index.php?n=2 [L]
RewriteRule ^services$ /index.php?n=3 [L]
RewriteRule ^contact$ /index.php?n=4 [L]

note the use of '/'
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Decrypt string by php 7 49
Dynamic Dropdowns 15 33
jQuery Toggle & Anchor Links 5 44
Adding additional variables to key => value association 17 17
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question