PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

.htaccess multiple rules

RewriteEngine On
RewriteBase /
# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/
# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?

I'm doing this code above in my .htaccess file, this works fine in my profile.php page because before, my profile.php is displaying this kind of URL:

http://mysite.com/profile.php?name=john

but when I wrote that code this is the result:

http://mysite.com/john/

I also wanted to apply this code to my category.php, so what I did was just to copy the code above and my whole .htaccess file looks like this one:

RewriteEngine On
RewriteBase /

# Use the following rule if you want to make the page like a directory
RewriteRule ^(v)$ /$1/

# The following rule does the rewrite.
RewriteRule ^(.+)/$ profile.php?name=$1
RewriteRule ^(.+)/$ category.php?cid=$1

# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} name=([^&]+)
RewriteRule ^profile.php$ %1?

RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} cid=([^&]+)
RewriteRule ^category.php$ %1?

But this code gives me a weird result because when I visit http://mysite.com/john/ the content that i'm seeing is the content of my category.php.

What should I do here? Any help would be greatly appreciated and rewarded!

Thanks!

Upvotes: 1

Views: 233

Answers (3)

jeroen
jeroen

Reputation: 91792

To answer the question as it was modified in the comments:

To differentiate between users and categories if these terms appear in the url, you only need one line per rule (untested example):

RewriteEngine On
RewriteBase /

RewriteRule ^user/(\w+)/?$ /profile.php?name=$1    // using only a limited set of chars (letters and underscore) for the user name and an optional / at the end

RewriteRule ^category/(\w+)/?$ /category.php?cid=$1    // using only a limited set of chars for the category (letters and underscore) and an optional / at the end

Upvotes: 1

PinoyStackOverflower
PinoyStackOverflower

Reputation: 5302

Looks like I solved it using this one:

RewriteEngine On
RewriteBase /

# Use the following rule if you want to make the page like a directory
RewriteRule ^user/(!(profile.php))$ user/$1/ [R=301,L]
RewriteRule ^category/(!(category.php))$ category/$1/ [R=301,L]

# The following rule does the rewrite.
RewriteRule ^user/(.+)/$ profile.php?id=$1
RewriteRule ^category/(.+)/$ category.php?id=$1

# The following rewrite the other way round:
RewriteCond %{REQUEST_URI} ^/profile.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /profile.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^profile.php$ user/%1?

RewriteCond %{REQUEST_URI} ^/category.php
RewriteCond %{THE_REQUEST} ^(GET|POST|HEAD|TRACE)\ /category.php
RewriteCond %{QUERY_STRING} id=([^&]+)
RewriteRule ^category.php$ category/%1?

Upvotes: 0

user1500341
user1500341

Reputation:

i have got the problem in ur rule. the problem what i suppose is that your webserver is unable to differentiate the request is of category page / profile page whenever an request comes like http://mysite.com/john/

so i suggest do one change in either profile page request or in category page request.

simply add http://mysite.com/profile/john/

hope this works out.

Upvotes: 1

Related Questions