.htaccess rules rely on order. If you plan on using multiple routes, save your htaccess rules and put your routes in PHP using one of several routing frameworks already written.
This explains why your .htaccess file does not work, line by line:
RewriteEngine on
This included RewriteEngine. There are no problems at the moment.
RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ [NC,OR] RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ RewriteCond %{REQUEST_URI} !wordpress/
Just a match RewriteRule is WordPress. This seems to work, so let it ignore this rule block.
RewriteRule (.*) /wordpress/$1 [L] Options +FollowSymLinks -MultiViews RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d
Only go below if the requested file name is not a file or directory.
RewriteRule .* index.php/$0 [PT,L]
Rewrite any PATH path in index.php / PATH . Stop processing if it matches (pay attention to L as last ). This means that nothing below will be activated.
#RewriteRule brandlisting/(.*)/ site/brandlisting?id=$1 [L]
Assuming it doesn't match .* (Aka, never), check the path for other patterns. Also note that the two lines you commented on and the two lines you don't have match the same patterns. If someone worked, the other will not.
- Pro Tip: Regex has an abbreviated expression for including a rule with a trailing slash and without it: /? equivalently, either with one slash or without a slash. Another way to write this is: /{0,1} .
How to fix
.htaccess Redirection rules are a pain. My rule is to make them as easy to write as possible, which makes them easier to read and maintain. How to do it? Direct redirection logic to your PHP program, rather than force Apache to rely on it.
Solution 1
The htaccess approach is only for you to understand that Apache rewrite flags tell your server and configure accordingly. You can do this here in one of two ways:
First create your more specific rules. those. move the rules /site/brandlisting?name=$1 to the rules .* .
Add separate rewrite conditions for any subsequent processing. According to the Apache2 documentation above:
The [L] flag causes mod_rewrite to stop processing the rule set. In most contexts, this means that if the rule matches, further rules will not be processed. This corresponds to the last command in Perl or the break command in C. Use this flag to indicate that the current rule should be applied immediately, without considering further rules.
The key point here is that it is inside each set of rules . The construction of a new set of rules will continue.
An example that should work (not verified):
RewriteEngine on RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ [NC,OR] RewriteCond %{HTTP_HOST} ^localhost/mate/admin$ RewriteCond %{REQUEST_URI} !wordpress/ RewriteRule (.*) /wordpress/$1 [L] Options +FollowSymLinks -MultiViews RewriteEngine On RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* index.php/$0 [PT,L]
There are some great built-in routers like Silex and Slim . If you do not want to use them, you can use your own internal logic, which analyzes various fragments. In my experience, the more I can break out of my .htaccess file, the happier you will be. I am sure that it is much easier to debug problems and easier to sort through changes without introducing unforeseen consequences.
Decision 2
You can use PHP routers in combination with the above .htaccess file, for example:
// Slim example $app = new Slim\Slim(); $app->get('/brandlisting/:key', function ($key) { if (is_numeric($key)) { $id = $key; // call/run $id logic } else { $name = $key; // call/run $name logic } }); // ... $app->run();
If you do, you can remove any of your logical brandlisting with .htaccess and put it in your index.php instead.
Conclusion
If you can help, experience tells me that itβs better not to write application logic in .htaccess rules. Instead, make your .htaccess rules simple and use a routing library. If you want to use .htaccess , make sure that you understand the Apache rewrite flags that you use, and that Apache reads everything from the top level bottom. For example, if the [L] flag is used, Apache stops reading all other rules in the ruleset . This means that you need to create a new set of rules with additional rules that need to be processed, or to introduce more specific rules first. Keep in mind that if these rules have the [L] flag, they will also stop the execution of any subsequent rules.