URL rewrite help

I am trying to learn Regex and URL Rewriting in PHP. How to create a .htaccess file that will rewrite (not redirect) any url to index.php?q={url}?

For instance:

http://www.example.com/baloon

to

http://www.example.com/index.php?q=baloon

I tried:

RewriteEngine On
RewriteRule ^$ index.php?q=$1 [R]

... but it does not work. What am I doing wrong?

Thank.

0
source share
1 answer

Rewriting ANY url in index.php? q = $ 1 will result in an internal server error, as this will create an infinite loop; instead, do something like this:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ index.php?q=$1 [L]
+3
source

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


All Articles