Redirect to dynamic relative paths with .htaccess?

Is it possible to make .htaccess "understand" dynamic relative paths and redirect them correctly?

My setup is as follows:

http://domain.com/htroot/aaa/xyz http://domain.com/htroot/bbb/xyz http://domain.com/htroot/ccc/xyz 

Etc. For example, "htroot" contains the .htaccess, which I need to change. The following sublevels (aaa, bbb, ccc) can be any name a-z0-9, and folders have index.php (or any other .php that needs to be redirected). xyz should work as a gender parameter, see the next part. The xyz part is nowhere in the file system as a “physical” folder or file.

I need to achieve the following: when you access from a URL

 http://domain.com/htroot/aaa/xyz 

he receives content from

 http://domain.com/htroot/aaa/ (or http://domain.com/htroot/aaa/index.php, either way) 

where index.php kicks into →, I can get xyz processed with REQUEST_URI and process it to serve the correct content that it sets, while the URL of the page remains http://domain.com/htroot/aaa/xyz , naturally.

So far I have managed to remove this if each sublevel (aaa, etc.) has its own .htaccess, but I need one where there is only one .htaccess located in htroot that handles this. I suppose this may have something to do with the $ 0 options in .htaccess, but not sure.

+4
source share
3 answers

You can do something like this:

 RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/(.*)$ /htroot/$1/index.php?data=$2 [L] 

If the first wildcard matches (.*) Is aaa and the second wildcard (.*) Is xyz (htroot / aaa / xyz), it will receive content from

htroot / aaa /index.php?data= hug

and you can get the xyz value in index.php with $_GET['data']

+3
source

Well, there’s something I don’t understand how it works, I think I still have a lot to learn about mod_rewrite.

But, as in the htroot / .htaccess file, it works:

 RewriteEngine On RewriteRule ^aaa/(.*)$ aaa/index.php?$1 [L,QSA] RewriteRule ^bbb/(.*)$ bbb/index.php?$1 [L,QSA] RewriteRule ^ccc/(.*)$ ccc/index.php?$1 [L,QSA] 

You will be able to access $ _GET ['xyz'] or what you put after / in your index.php scripts. You will also get a bonus entry "index_php" in the $ _GET array, but I assume that this is an internal redirect.

+1
source

This should work as a general set of rules:

 RewriteEngine On RewriteCond %{REQUEST_URI} !\.php RewriteRule ^(.*)/(.*) /htroot/$1/index.php?parameters=$2 [L,QSA] 
+1
source

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


All Articles