1

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...

MrWhite
  • 12,647
  • 4
  • 29
  • 41

1 Answers1

0

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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41