Link to home
Start Free TrialLog in
Avatar of narmi2
narmi2

asked on

URL Rewriting HTACCESS

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?
Avatar of richammond
richammond
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of narmi2
narmi2

ASKER

Hello,

Could you please explain what's happening in the script?
Avatar of narmi2

ASKER

That script does not work.  It makes all my existing re-written urls go to the home page index.php.
this should work, haven't tested it, but its defenatly the right direction.

RewriteRule ^/page.php?category=([^&]+)[^=]=([^&]+)[^=]=([^&]+)$ /$1/$2/$3
Avatar of narmi2

ASKER

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?
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.
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
Avatar of narmi2

ASKER

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
whats the result, i cant find the mistake if i dont know the result?
Avatar of narmi2

ASKER

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
Avatar of narmi2

ASKER

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...
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
Avatar of narmi2

ASKER

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
Avatar of narmi2

ASKER

I asked the above, because it gives me an internal server error message when I try to access any part of my site...
i think you dont need my conition because of your first one.

sorry it should have been: RewriteCond %{REQUEST_FILENAME} !^.*\.css
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]+)
Avatar of narmi2

ASKER

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
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]
Avatar of narmi2

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of 1eEurope
1eEurope
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you could also define aliases for images and css.
Avatar of narmi2

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial