BeeTricks BeeTricks - 1 month ago 9
Apache Configuration Question

How can i rewrite this url with a .htaccess file?

can someone explain me how i can rewrite this url:

localhost:8080/log_reg/log_reg/index.php?page=register


to this:

localhost:8080/log_reg/log_reg/page/register



i'm using vagrant for testing

i already tested this:

RewriteEngine On
RewriteRule ^pages/([^/.]+)/([^/.]+)?$ log_reg/index.php?page=$1 [L,NC]


i know that this question is posted like 200x here. but i don't know how to get this working on my example... can anyone correct the htaccess-code?

Answer

Very close. But at least you have to make sure that the slash is also optional:

RewriteEngine On
RewriteBase /
RewriteRule ^log_reg/page/([^/.]+)(/[^/.]+)?$ /log_reg/index.php?page=$1 [L,NC]

Or the more relaxed version:

RewriteEngine On
RewriteBase /
RewriteRule ^log_reg/page/([^/.]+) /log_reg/index.php?page=$1 [L,NC]

For that to work the file has to be located inside the outer /log_reg folder inside your DocumentRoot.

In general you should prefer to place such rules inside your http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided for situations where one does not have access to the host configuration (read: really cheap hosting providers) or for applications that need to write their own rewriting rules (which is an obvious security nightmare).