Afonso Gomes
Afonso Gomes

Reputation: 934

Simple mod_rewrite in existing website

I'm doing a website (for portuguese speaking countries) that displays phrases one at a time.

I would like to have simpler non-dynamic addresses for the site to be SEO friendlier and easier to memorize to the user. So my problem is this, and thanks in advance for all the help possible:

nfrases.com/ -> completely random phrase (don't need changing this!)
nfrases.com/index.php?id_frase=2222 -> phrase with ID (wanted this to be nfrases.com/2222)
nfrases.com/tag.php?tag_nome=amor -> random phrase with tag_nome (want: nfrases.com/amor)
nfrases.com/tag.php?tag_nome=amor&id_frase=2222 -> specific phrase with tag=amor (want: nfrases.com/amor/2222)

I think this is a noob question about mod_rewrite but that's exactly what I am! :D Already made several tries with the few knowledge I have but either i got 404 pages either the parameters wouldn't be received by the $_GET['tag_nome'] or $_GET['id_frase'].

The actual htaccess I have is this but it isn't working:

Options +FollowSymlinks -MultiViews

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.nfrases.com$
RewriteRule ^(.*)$ http://www.nfrases.com/$1 [R=301]

RewriteRule ^([0-9]+)(.*) index.php?id_frase=$1
RewriteRule ^(.*)/([0-9]+) tag.php?tag_nome=$1&id_frase=$2 [L,QSA]

Can you guys help me?

Thanks again for everything! I appreciate your help.

Upvotes: 0

Views: 178

Answers (2)

Levi Morrison
Levi Morrison

Reputation: 19552

The following rewrite rules work for me on a RewriteRule tester:

#removes trailing slash
#/vida/222/ becomes /vida/222
#/222/ becomes /222
RewriteRule ^(.+)/$ /$1 [R=301]

#/222 silently rewrites to /index.php?id_frase=222
RewriteRule ^([0-9]+)$ index.php?id_frase=$1 [L]

#/vida silently rewrites to /index.php?tag_nome=vida
RewriteRule ^([a-zA-Z-]+)$ index.php?tag_nome=$1 [L]

#/vida/222 silently rewrites to /index.php?tag_nome-vida&id_frase=222
RewriteRule ^([^/]+)/([0-9]+)$ tag.php?tag_nome=$1&id_frase=$2 [L]

Upvotes: 1

Rodrigo Ferreira
Rodrigo Ferreira

Reputation: 366

You may want something like this:

<IfModule mod_rewrite.c>
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

In this case, nfrases.com/2222 will rewrited as index.php/2222. This easily allows base routing in your index.php file. For more advanced routing, it may be faster and easier to use a good framework. Take a look at FuelPHP (http:www.fuelphp.com) or CodeIgniter (http://www.codeigniter.com).

Upvotes: 1

Related Questions