Answer the question
In order to leave comments, you need to log in
mod_rewrite and PHP rules?
Faced with a question that I do not fully understand
There is a site in PHP. They asked me to write rewrite rules, but I ran into PHP behavior that I don't understand.
So what is needed. So that the link example.com/forum/blog/0/lalala be shortened to example.com/blog/lalala
That is, a person types in the browser example.com/blog/lalala and this link opens without changing the path in the browser but is processed along the path example.com/forum/blog/0/lalala
And there are 2 .htaccess files, one along the path /.htaccess, the other /forum/.htaccess (relative to the site root)
Both are used for the
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
When trying to use the
RewriteCond %{REQUEST_URI} ^/blog/(.+)$ [NC]
RewriteRule ^(.*)$ /forum/blog/0/%1 [L]
rule, it redirects to the /forum/index.php handler , but only the primary URI is passed into it. Those. print REQUEST_URI from php shows /blog/lalala
And if you turn off RewriteRule ^(.*)$ index.php?/$1 [L] then it shows 404 with path /forum/blog/0/lalala.
It turns out when all the rules are enabled - it rewrites the address, makes an internal redirect, but PHP still gets the primary address from the browser!
And if I include the R flag in my rule - that is, a redirect, then everything works, but at an unabbreviated address.
And here's the question - is this even possible - without a redirect visible to the user to do this? Some kind of address remap?
Answer the question
In order to leave comments, you need to log in
It turned out that after Internal Redirect in mod_rewrite, the new rewritten link is contained in REDIRECT_URL. So, in fact, the problem was solved. Handler in script fixed to use REDIRECT_URL along with REQUEST_URI}
but I ran into a PHP behavior that I don't understand.
RewriteRule ^\/blog\/(.*)\/?$ /forum/blog/0/$1 [QSA,L]
to all your specified rules.
Place the new rule above the old ones. remove L from it, it turns out that the new one will rewrite the url to the old one, then the old one will calmly be called, which will direct everything to index.php.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question