Solved

URL Rewriting HTACCESS

Posted on 2008-10-30
25
369 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

724 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