Htaccess rewrite rule to redirect to file or not

I am trying to write a set of htaccess rules that will do the following:

  • From URL: mydomain.com/blog/something

    • If a file called blog.php exists in the
      web directory (a directory with index.php in), then redirect to blog.php? Url = something

    • If blog.php does not exist, redirect to index.php? url = blog / something

    • EDIT: mydomain.com/blog/something is an example, it can be any url line, and htaccess should look in the web directory for the file corresponding to the first part of the url line, which in this case is a “blog”

In both cases, I can use $ _GET ['url'] to get the parameters from the url to set any actions in my php script.

I currently have:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^([^/]*)/(.*)$ $1.php?url=$2 [L]
RewriteRule ^(.*)$ index.php?url=$1 [L]

</IfModule>

, , , , , php, htaccess . , [L] , , . , url, $_GET ['url'] "index.php".

, .

, -

+3
1

, .

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?.*$ - [E=FILE:%{DOCUMENT_ROOT}/$1.php]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} -f
RewriteRule ^([^/]*)/?(.*)$ $1.php?url=$2 [QSA,L]

RewriteCond %{ENV:FILE} !^$
RewriteCond %{ENV:FILE} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>

Apache mod_rewrite : http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

+2

Source: https://habr.com/ru/post/1749164/


All Articles