Is PHP allowed to modify the .htaccess file in the current folder?

I have a PHP web application hosted on a shared hosting . My goal is to modify the .htaccess file from the PHP code when starting the PHP page. I need this .htaccess to insert a couple of mod_rewrite lines into it.

The problem is that on Windows + Apache I can change the .htaccess file dynamically but the same code on Linux reports a problem when I try to access this file in any (copy or fopen):

"failed to open stream: Permission denied"

I gave the .htaccess file 777 permissions - there is still no result. What is stopping me from doing this? How can I develop a workaround?

PS My initial goal was to add a new RewriteRule to .htaccess, which displays the added category_id category with a new category name.

If this were not shared hosting, I would use something like RewriteMap (in the main Apache config) and could access the map file.

This is the first real limitation that I could not handle with PHP + Apache, but I hope this is also possible.

+3
source share
5 answers

I want to offer something else that works too. Instead of writing a rule for each "special" URL, why not use it for everyone?

It was a lot easier for me to use what Wordpress uses: each URL is redirected to an index.

, , , (, $_SERVER ['URI_REQUEST']) .

.htaccess :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

chunck , . - URL- , !

+5

, URL- .

, URL-:

http://yoursite.com/category/programming

- :

http://yoursite.com/category.php?name=programming
+7

. , :

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/category/.*$ category.php [QSA]

, , - /category/category_id, category.php, URI /category/ $_SERVER['REQUEST_URI'], , .htaccess , .

+3

, . PHP-, WordPress, , . , , NEEDS - .

, 777 775 , Apache, ( "apache" "www" - )

( ) , , , , , .

+3

. , .

FYI, one of the reasons that rewriting .htaccess is a bad idea is that any requests that come in while .htaccess are rewriting, no redirects will be applied.

+1
source

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


All Articles