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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.