Rewriting a URL to a query string on Apache and Nginx Rewriting a URL to a query string on Apache and Nginx nginx nginx

Rewriting a URL to a query string on Apache and Nginx


In nginx you match "/location" in a rewrite directive, capture the tailing string in the variable $1 and append it to the replacement string.

server {...rewrite ^/location(.*)$ /subdirectory/index.php?content=location$1 break;...}

In Apache's httpd.conf this looks quite similar:

RewriteEngine OnRewriteRule ^/location(.*)$ /subdirectory/index.php?content=location$1 [L]

Have a look at the examples at the end of this page: https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html


Search string: (.+)/location/(.*)$

replacement string: $1/subdirectory/index.php?content=location/$2


For Apache, in the htaccess file in your document root, add:

RewriteEngine OnRewriteCond %{REQUEST_URI} !^/subdirectory/index\.php$RewriteRule ^(.*)$ /subdirectory/index.php?content=$1 [L]

In nginx, you want to first make sure requests for /subdirectory/index.php get passed through, then rewrite everything else:

location ~ /subdirectory/index\.php$ { } location / {     rewrite ^(.*)$ /subdirectory/index.php?content=$1 break; }