mod_rewrite, .htaccess, creating a map

gutterseo

▬▬▬▬▬▬▬&
Feb 27, 2009
1,271
27
48
Does anyone know where I can find a good simple tutorial (or explain to me here) how to use a map with mod rewrite.

Its for a cms that I am working on and I don't feel like modifying the .htaccess file directly after a new page is added.

Have looked around the internet and couldn't get the tutorial on sitepoint to do what I want.

Reppage for anyone who points me in the right direction.
 
  • Like
Reactions: chatmasta


Well it always happens to me that I find the soln ten minutes after making a post. May as well document it here if anyone needs info in future.

In the httpd.conf file or if using vhosting in apache2/sites-available/file enter
Code:
    RewriteEngine on
    RewriteMap pids /path/to/url/map
    RewriteRule ^(.*)$ /index.php?id=${pids:$1|0}

where urls.txt is a map file in this format
/ 0
/about 1
/order 2
....

Restart apache and if you don't get any errors you should be good to go. Very simple yes, headwrecking yes also.
 
Like the second poster, i've no idea what you're on about and looking at your solution hasn't increased my understanding. :)
 
Like the second poster, i've no idea what you're on about and looking at your solution hasn't increased my understanding. :)
Its basically an external file that modrewrite looks up to perform the rewrite of a url, instead of having that information in the .htaccess file.

I find it handy when you have a website that will be updated periodically by other non technical users and you want clean SEO friendly urls. A map file matching url to id is a lot easier for a script to update than to update the .htaccess with a rewriterule.

If you have directories or urls that you dont want rewritten include them like this before the rewritemap rule

Code:
    RewriteCond %{REQUEST_URI} !^/admin # to exclude admin directory
    RewriteCond %{REQUEST_URI} !^/includes # to exclude includes directory
 
Its basically an external file that modrewrite looks up to perform the rewrite of a url, instead of having that information in the .htaccess file.

I find it handy when you have a website that will be updated periodically by other non technical users and you want clean SEO friendly urls. A map file matching url to id is a lot easier for a script to update than to update the .htaccess with a rewriterule.

If you have directories or urls that you dont want rewritten include them like this before the rewritemap rule

Code:
    RewriteCond %{REQUEST_URI} !^/admin # to exclude admin directory
    RewriteCond %{REQUEST_URI} !^/includes # to exclude includes directory

Ah that's cool, thanks for answering. :)

I'm being curious here, not being a cock for the record. Why is that easier then say, having htaccess use regex to find a variable within the url and do it that way? If you access pages based on an specific id (page.php?id=1 etc), then if you use a function on page.php that checks it's own url against the variables pulled from id=1 in the database, and 301s if not correct, then surely htacces never needs upating. You know?

Say like this for instance.

URL domain.com/A-1/tits-and-arse-pics.html

htaccess RewriteRule ^A-([0-9]*)/.*\.html$ page.php?id=$1 [L]

Then on page, have function to check the url: Fetch all data according to id, build the url, see if matches the actual url, if does then good, if not, 301 redirect to correct url.
 
Simply because i don't want variables in my urls.
I want a structure like:

www. somesite .com/pagetitle
 
  • Like
Reactions: -God-
Haha, good answer. And actually, a fucking superb reason for using it. Thanks. +rep :)