Jump to content

url rewriting for SEO


scarhand

Recommended Posts

If you know regular expressions, then it's pretty trivial.

 

First you start by enabling rewriting:

RewriteEngine on

 

Then to rewrite e.g. /profile/Daniel0 to index.php?act=profile&username=Daniel0 you can do this:

RewriteRule ^/profile/([a-zA-Z0-9]+)$ index.php?act=profile&username=$1

 

See, it's separated by spaces. The second part is the regex, i.e. the URL from the user to match. In regular expressions, everything in parentheses are stored in backreferences. The third part is the URL to rewrite to and $1 will here be replaced by the username. After that you can optionally have some options (flags), such as redirection, stop looking for more entries, send a specific response code, etc. You can also set various conditions that should be met, e.g. that HTTPS must be on. You can read about it here and here.

 

If you're starting from scratch, then this might be better:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f # condition: only if the requested URL isn't a file that exists
RewriteRule .* index.php # forward everything to index.php

 

Then you can get the requested URL from $_SERVER['REQUEST_URI'] and then you can split it up or you can use regex there to send the request data to whatever place that should take care of that specific request.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.