Score:1

Two different rewrite conditions in the same .htaccess?

es flag

I need to create a rewrite condition for two specific files, according to the requested path in the URL. It's possible?

Condition 1: If the URL is example.com/admin go to admin.php?uri=params. That is:

  • example.com/admin/form => admin.php?uri=form

  • example.com/admin/form/edit => admin.php?uri=form/edit

etc...

Condition 2: If the URL takes ANY OTHER parameter (other than /admin), go to index.php?uri=params. Thus:

  • example.com/about => index.php?uri=about

  • example.com/test => index.php?uri=test

  • example.com/blau/bleu => index.php?uri=blau/bleu

etc...

Score:0
kz flag

Try something like this:

DirectoryIndex index.php

Options -MultiViews

RewriteEngine On

RewriteRule ^admin(?:$|/([^.]*))$ admin.php?uri=$1 [QSA,L]

RewriteRule ^([^.]+)$ index.php?uri=$1 [QSA,L]

This does assume that none of your URLs (that need rewriting) contain dots (.). This avoids the need for a filesystem check to make sure the request does not map to a static resource, that would normally include a file extension (delimited by a dot).

MultiViews needs to be disabled (if not already) since you are requesting a URL /admin that is the basename of the file you are rewriting to. If MultiViews was enabled then /admin.php would be called without the URL parameter.

The QSA (Query String Append) flag is only required if you are expecting a query string on the original request, that would need to be appended to the rewritten URL.

A request for the document root (homepage) will call index.php without the uri parameter.


Aside: Just a note on terminology...

If the URL takes ANY OTHER parameter (other than /admin)

What you are calling "parameter" here is really the URL-path (or path-segments). URL Parameters are the name=value pairs in the query string (what you are wanting to rewrite the request to).

In your example, you are rewriting the URL-path to a URL parameter value.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.