Chibuzo
Chibuzo

Reputation: 6117

How do I resolve the conflict of rules in this .htaccess file?

Each of the rules in the file works well if I comment out the other, but the two of them together do not achieve either purpose but rather renders the site ugly. Any suggestion on how to resolve this? Thanks.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
# do not do anything
RewriteRule ^ - [L]

# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

# forward /john to user_page/profile.php?name=john
RewriteRule ^((?!blog/).+)$ user_page/profile.php?uid=$1 [L,QSA,NC]

Upvotes: 1

Views: 159

Answers (2)

Chibuzo
Chibuzo

Reputation: 6117

I've finally figured this out. I modified the second rewrite condition because the second rewrite rule redirects to a url that is not necessary valid i.e www.example.com/username to the intended valid url which looks like this www.example.com/user_page/profile.php?user=username.

I also modified the last/second rewrite rule as Safarov suggested and it worked! Below is the full .htaccess file.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# OR If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f  [OR]
# OR If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
# do not do anything
RewriteRule ^ - [L]

# forward /blog/foo to blog.php/foo
RewriteRule ^blog/(.+)$ blog.php/$1 [L,NC]

# forward /john to user_page/profile.php?name=john
RewriteRule ^(.+)$ user_page/profile.php?uid=$1 [L,QSA]

Upvotes: 1

safarov
safarov

Reputation: 7804

Try replace RewriteRule ^((?!blog/).+)$ with RewriteRule ^(.+)$ there is no need to check for blog string again

Upvotes: 2

Related Questions