Mod Rewrite Query

Soldato
Joined
18 Oct 2002
Posts
5,464
Location
London Town
It's certainly possible, but you need to have some kind of mapping between which /pagename relates to which /index.php?pageid=x uri. Does this need to be dynamic or just for this one instance? Do you have a way to change index.php so that it can select based on 'pagename' rather than ID?

For example:

  • Rewrite /pagename to /index.php?name=pagename and have your index.php script select the correct content
  • Have a URL like /1/pagename rewrite to /index.php?pageid=1
  • Have separate explicit rewrite rules for every /pagename to /index.php?pageid=x mapping (cf. RewriteMap)

Need further information to be able to suggest the best rewrite rule.
 
Soldato
Joined
18 Oct 2002
Posts
5,464
Location
London Town
To rewrite /pagename to /index.php?page=pagename:
Code:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]

RewriteRule ^([^/]+)/? index.php?page=$1 [L]

The set of Rewrite Conditions (RewriteCond) ensure that if a real file or directory exists with the requested name, it won't get rewritten by the later rule e.g. your images or css directory. The last rule is the one which rewrites anything like /pagename to /index.php?page=pagename with an optional trailing slash (/?).
 
Back
Top Bottom