Solved

URL Rewriting HTACCESS

Posted on 2008-10-30
25
353 Views
Last Modified: 2012-05-05
Dear Experts,

I have the following type of url

http://www.narmi.com/page.php?category=1&surname=smith&forename=smith

How do I re-write that using .htaccess so it looks something like this

http://www.narmi.com/service-type=goes-here/category-type-goes-here/the-team/john-smith.php

For example

http://www.narmi.com/organisation/charity-services/the-team/john-smith.php



category 1 = charity services in the database

category 2 = some other service

category 3 and so on

Is this possible?
0
Comment
Question by:narmi2
  • 12
  • 10
  • 2
  • +1
25 Comments
 
LVL 7

Expert Comment

by:richammond
ID: 22839032
Hi,
Try this :

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php

Regards
0
 
LVL 1

Author Comment

by:narmi2
ID: 22839062
Hello,

Could you please explain what's happening in the script?
0
 
LVL 1

Author Comment

by:narmi2
ID: 22839888
That script does not work.  It makes all my existing re-written urls go to the home page index.php.
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22840515
this should work, haven't tested it, but its defenatly the right direction.

RewriteRule ^/page.php?category=([^&]+)[^=]=([^&]+)[^=]=([^&]+)$ /$1/$2/$3
0
 
LVL 1

Author Comment

by:narmi2
ID: 22840633
It doesn't work.

I think I need to change the url to something like this

http://www.narmi.com/organisation/charity-services-1/the-team/john-smith.php

so the category 1 is in the new url after charity-services

How do I do that?
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22840693
thy this: RewriteRule ^/page.php?category=([^&]+)[^=]=([^&]+)[^=]=([^&]+)[^=]=([^&]+)$ /organisation/$2/$1/$4-$3.php

post me the url you tried after and before the redirect so i can see what happend.
0
 
LVL 1

Author Comment

by:narmi2
ID: 22841158
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22841316
I have been doing it back to front :-( its easier this way round

RewriteRule ^organisation/[^0-9]([0-9])/the-team/([a-z])-([a-z]).php page.php?category=$1&forename=$2&surname=$3
0
 
LVL 1

Author Comment

by:narmi2
ID: 22841901
Still doesn't work.  I have also tried this:

RewriteRule ^organisation/[a-z-]+-([0-9]+)/the-team/([a-z]+)-([a-z]+).php page.php?category=$1&forename=$2&surname=$3
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22841936
whats the result, i cant find the mistake if i dont know the result?
0
 
LVL 1

Author Comment

by:narmi2
ID: 22842016
It seems to give me the sitemap for my site without any css.  So it's almost like it is trying to find the physical path organisation/charity-law-1/the-team/

which is not what it should be doing.

I have another rule in the .htaccess file which redirects to the sitemap if the url typed is not found i.e.

ErrorDocument 404 /sitemap.php
0
 
LVL 1

Author Comment

by:narmi2
ID: 22842085
I did the following as a test:

RewriteRule ^organisation/charity-law-1/the-team/john-smith.php page.php?category=1&forename=john&surname=smith [L]

and typed in the following url into the browser

http://www.narmi.com/organisation/charity-law-1/the-team/john-smith.php

and it gave me the correct page but with no formatting, i.e. no css...
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:1eEurope
ID: 22842236
you have to exclude the .css files from the rule before your rule

RewriteCond %{REQUEST_FILENAME} !^*.css
RewriteRule ^organisation/[a-z-]*([0-9]+)/the-team/([a-z]+)-([a-z]+).php page.php?category=$1&forename=$2&surname=$3
0
 
LVL 1

Author Comment

by:narmi2
ID: 22842290
My css files are in the root along with page.php i.e.

www.narmi.com/page.php
www.narmi.com/style.css

So with this in mind, is the following correct?

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^*.css
RewriteRule ^organisation/[a-z-]+-([0-9]+)/the-team/([a-z]+)-([a-z]+).php page.php?category=$1&forename=$2&surname=$3
0
 
LVL 1

Author Comment

by:narmi2
ID: 22842346
I asked the above, because it gives me an internal server error message when I try to access any part of my site...
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22842389
i think you dont need my conition because of your first one.

sorry it should have been: RewriteCond %{REQUEST_FILENAME} !^.*\.css
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22842412
a very small mistake i pointed out before: RewriteRule ^organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+).php page.php?category=$1&forename=$2&surname=$3

NOT: [a-z-]+-([0-9]+)
0
 
LVL 1

Author Comment

by:narmi2
ID: 22842736
I have made those changes which is now giving better results but still missing the css formatting on the page.

I have tried:

RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^.*\.css
RewriteRule ^organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+).php page.php?category=$1&forename=$2&surname=$3 [L]

and this:

RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+).php pagey.php?category=$1&forename=$2&surname=$3 [L]

both do the same thing, i.e. correct page but no css formatting?

Typed URL = http://www.narmi.com/organisation/charity-law-123/the-team/john-smith.php
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22842854
RewriteCond %{REQUEST_URI} (\.htm|\.php|\.html)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+)\.php pagey.php?category=$1&forename=$2&surname=$3 [L]
0
 
LVL 1

Author Comment

by:narmi2
ID: 22842944
Changed to (showing full script)

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (\.htm|\.php|\.html)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+).php page.php?category=$1&forename=$2&surname=$3 [L]

But still giving same error as before, i.e. correct page but no css formatting.

css formatting works when I type

www.narmi.com/page.php?category=123&forename=john&surname=smith

but does not work if I do

http://www.narmi.com/organisation/charity-law-123/the-team/john-smith.php
0
 
LVL 1

Accepted Solution

by:
1eEurope earned 210 total points
ID: 22842973
try this: RewriteRule ^/organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+)\.php /page.php?category=$1&forename=$2&surname=$3 [L]
0
 
LVL 27

Assisted Solution

by:caterham_www
caterham_www earned 290 total points
ID: 22843611
>>RewriteCond %{REQUEST_URI} (\.htm|\.php|\.html)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d<<

That is useless since none of your files matches the regular expression ^organisation/[a-z-]+([0-9]+)/the-team/([a-z]+)-([a-z]+).php  which is being processed *before* any conditions are applied. So an existing file 'foo.jpg' or whatever will never match the rule-pattern.

The problem - missing ressources - is not caused by mod_rewrite or apache. Your references are pointing to a wrong location. Remember: If you're requesting a page like /organisation/charity-law-123/the-team/john-smith.php you're within the level /organisation/charity-law-123/the-team/. If you've a img src="foo.jpg" on that page, the full URL-path built and requested by your browser would be /organisation/charity-law-123/the-team/foo.jpg, which doesn't resolve to an existing file on server-side, of course.

Change your html references into an absolute path, e.g. src="/foo.jpg" (leading slash) or use <base href="http://example.com/" /> in your <head> section [or don't use a non-existent folder structure at all, which creates this problem].
0
 
LVL 1

Expert Comment

by:1eEurope
ID: 22848043
you could also define aliases for images and css.
0
 
LVL 1

Author Comment

by:narmi2
ID: 22848869
Ok, by adding a / before all images and css files in the html seems to have fixed the problem.  However, all links on that page no longer work.

For example, if I click home page, instead of going to http://www.narmi.com/index.php, goes to http://www.narmi.com/organisation/charity-law-1/index.php

How do I stop that from happening?
0
 
LVL 27

Assisted Solution

by:caterham_www
caterham_www earned 290 total points
ID: 22849155
It looks like you have a 'href="index.php"', which is a relative link,  instead of 'href="/index.php"'. That applies to all linked/referenced resources.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now