m3tsys
m3tsys

Reputation: 3969

Why this htaccess code is not working as it should?

I have a problem with my htaccess code.

When i try to load example.com/user/register/ the page that is readed is profile.php

When i acces example.com/user/register it works fine and it reads register.php

How can i edit this htaccess code to use example.com/user/register/ ?

RewriteRule ^user/register?$ register.php [L]
RewriteRule ^user/register?$ /user/register/ [R=301,L]

RewriteRule ^user/?$ user.php [L]
RewriteRule ^user?$ /user/ [R=301,L]

RewriteRule ^user/(\d+)/$ profile.php?id=$1 [L]
RewriteRule ^user/(\d+)?$ /user/$1/ [R=301,L]

RewriteRule ^user/([^/]+)/$ profile.php?username=$1 [L]
RewriteRule ^user/([^/]+)?$ /user/$1/ [R=301,L]

Upvotes: 0

Views: 89

Answers (1)

Dan
Dan

Reputation: 5637

Are you after using /user/register/ or /user/register?

From the looks of your current rewrites, you're rewriting /user/register to point to /register.php and then redirecting it to /user/register/ although that redirect would never take place because of the [L] in the previous rule.

If you want to use /user/register/ I would suggest trying:

RewriteRule ^user/register/$    /register.php [L]
RewriteRule ^user/register$     /user/register/ [R=301,L]

If you want /user/register you should need:

RewriteRule ^user/register$     /register.php [L]
RewriteRule ^user/register/$    /user/register [R=301,L]

Alternatively, if you don't care whether the URL has a trailing slash or not, this would do:

RewriteRule ^user/register/?&   /register.php [L]

Another point to consider would be that if you want none of your URLs to have trailing slashes, you could use a generic redirect at the top of your .htaccess rather than a redirect for each page:

RewriteRule ^(.*)/$    /$1 [R=301,L]

Upvotes: 1

Related Questions