Igor
Igor

Reputation: 673

Friendly URL working but not rewriting

I've the following .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Rewrite www.site.com.br/usuarios.php?user=Igor&area=inicio to
    # www.site.com.br/Igor/inicio
    RewriteCond %{THE_REQUEST} /usuarios\.php\?user=([^&\s]+)&area=([^&\s]+) [NC] 
    RewriteRule ^ /%1/%2? [R=302,L,NE]

    # Rewrite www.site.com.br/usuarios.php?user=Igor to
    # www.site.com.br/Igor if not have the second parameter
    RewriteCond %{THE_REQUEST} /usuarios\.php\?user=([^&\s]+) [NC] 
    RewriteRule ^ /%1? [R=302,L,NE]

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

    RewriteRule ^((?!usuarios/|posts/).*)$ usuarios.php?q=$1 [L,NC]

    # Rewrite www.site.com.br/posts.php?post=HelloWorld to
    # www.site.com.br/posts/HelloWorld
    RewriteRule ^posts/(.*) posts.php?post=$1 [L,QSA]
</IfModule>

My problem is on the last RewriteRule: RewriteRule ^posts/(.*) posts.php?post=$1 [L,QSA]. If I access www.site.com.br/posts/HelloWorld it works perfectly, and if I access www.site.com.br/posts.php?post=HelloWorld works too but not rewrite to www.site.com.br/posts/HelloWorld.

How to do this?

Upvotes: 1

Views: 23

Answers (1)

anubhava
anubhava

Reputation: 785226

You will need one more redirect rule

<IfModule mod_rewrite.c>
    Options -MultiView
    RewriteEngine On

    # Rewrite www.site.com.br/usuarios.php?user=Igor&area=inicio to
    # www.site.com.br/Igor/inicio
    RewriteCond %{THE_REQUEST} /usuarios\.php\?user=([^&\s]+)&area=([^&\s]+) [NC] 
    RewriteRule ^ /%1/%2? [R=302,L,NE]

    RewriteCond %{THE_REQUEST} /posts\.php\?post=([^&\s]+) [NC] 
    RewriteRule ^ /post/%1? [R=302,L,NE]

    # Rewrite www.site.com.br/usuarios.php?user=Igor to
    # www.site.com.br/Igor if not have the second parameter
    RewriteCond %{THE_REQUEST} /usuarios\.php\?user=([^&\s]+) [NC] 
    RewriteRule ^ /%1? [R=302,L,NE]

    # Rewrite www.site.com.br/posts.php?post=HelloWorld to
    # www.site.com.br/posts/HelloWorld
    RewriteRule ^posts/(.*) posts.php?post=$1 [L,QSA]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^((?!usuarios/|posts/).*)$ usuarios.php?q=$1 [L,NC]

</IfModule>

Upvotes: 1

Related Questions