Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 377
  • Last Modified:

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?
0
narmi2
Asked:
narmi2
  • 12
  • 10
  • 2
  • +1
3 Solutions
 
richammondCommented:
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
 
narmi2Author Commented:
Hello,

Could you please explain what's happening in the script?
0
 
narmi2Author Commented:
That script does not work.  It makes all my existing re-written urls go to the home page index.php.
0
Independent Software Vendors: 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!

 
1eEuropeCommented:
this should work, haven't tested it, but its defenatly the right direction.

RewriteRule ^/page.php?category=([^&]+)[^=]=([^&]+)[^=]=([^&]+)$ /$1/$2/$3
0
 
narmi2Author Commented:
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
 
1eEuropeCommented:
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
 
narmi2Author Commented:
0
 
1eEuropeCommented:
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
 
narmi2Author Commented:
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
 
1eEuropeCommented:
whats the result, i cant find the mistake if i dont know the result?
0
 
narmi2Author Commented:
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
 
narmi2Author Commented:
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
 
1eEuropeCommented:
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
 
narmi2Author Commented:
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
 
narmi2Author Commented:
I asked the above, because it gives me an internal server error message when I try to access any part of my site...
0
 
1eEuropeCommented:
i think you dont need my conition because of your first one.

sorry it should have been: RewriteCond %{REQUEST_FILENAME} !^.*\.css
0
 
1eEuropeCommented:
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
 
narmi2Author Commented:
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
 
1eEuropeCommented:
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
 
narmi2Author Commented:
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
 
1eEuropeCommented:
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
 
caterham_wwwCommented:
>>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
 
1eEuropeCommented:
you could also define aliases for images and css.
0
 
narmi2Author Commented:
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
 
caterham_wwwCommented:
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

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 12
  • 10
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now