Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple htaccess rewrite rule

here is my code for .htaccess file

Options -Indexes
RewriteEngine On
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^([a-z0-9]+)$ /profile.php?username=$1 [L]
RewriteRule ^([a-z0-9]+)$ /display.php?page=$1 [L]

For the first one it works correctly and is displaying like this: www.site.com/user

The second one is not working, normally is displaying like this www.site.com/display.php?page=10. I want to display the page like this www.site.com/article I tried different things and no result. Please tell me how to make to work with multiple rules. Also please give me an advice on how to use this functionalities in php because I think I done something not really good. My php code for using this rule is:

<p><a class="button" href="/<?php echo $user_data['username']; ?>"> Profile</a></p>

It works, but maybe is a better way to make a link to take advantage of htaccess.

like image 615
Catalin Macovei Avatar asked Jul 01 '13 19:07

Catalin Macovei


People also ask

What is RewriteCond and RewriteRule?

RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive. If we talk about traditional programming RewriteCond works just like 'If' condition where you can use conditions like AND, OR, >=, == , !

What is rewrite engine on htaccess?

htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.

What is IfModule mod_rewrite C?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.


2 Answers

The two rules that you have conflict, the patterns used are exactly the same, which means, other than the conditions which only get applied to the first rule, the two rules are completely indistinguishable.

Given this URL:

http://www.site.com/blah

Is "blah" a page or a user? Can't tell, because the regex pattern (^([a-z0-9]+)$) for both rules matches "blah". So, the first one will always get applied no matter what. You need to add something to distinguish between the 2, like including a "user" or "page" in the URL:

http://www.site.com/user/blah
http://www.site.com/page/bleh

And your rules would look like:

Options -Indexes
RewriteEngine On

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^user/([a-z0-9]+)$ /profile.php?username=$1 [L]

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^page/([a-z0-9]+)$ /display.php?page=$1 [L]
like image 155
Jon Lin Avatar answered Sep 19 '22 15:09

Jon Lin


Options -Indexes
RewriteEngine On

RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule ^user/([a-z0-9]+)$ /profile.php?username=$1 [N]
RewriteRule ^page/([a-z0-9]+)$ /display.php?page=$1 [L]

use [N] (next) continue adding rules to the same condition and [L] (last) to identify the last rule

like image 38
Guilherme Pichok Avatar answered Sep 22 '22 15:09

Guilherme Pichok