Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rewriting a URL to a query string on Apache and Nginx

I'm trying to set up some path rewrites on two separate servers, one using mod-rewrite on Apache and one using HttpRewriteModule on Nginx. I don't think I'm trying to do anything too complex, but my regex skills are a little lacking and I could really use some help.

Specifically, I'm trying to transform a formatted URL into a query string, so that a link formatted like this:

http://www.server.com/location/

would point to this:

http://www.server.com/subdirectory/index.php?content=location

Anything extra at the end of the formatted URL should be appended to the "content" parameter in the query string, so this:

http://www.server.com/location/x/y/z

should point to this:

http://www.server.com/subdirectory/index.php?content=location/x/y/z

I'm pretty sure this should be possible using both Apache mod-rewrite and Nginx HttpRewriteModule based on the research I've done, but I can't see to get it working. If anyone could give me some pointers on how to put together the expressions for either or both of these setups, I'd greatly appreciate it. Thanks!

like image 672
user1755011 Avatar asked Oct 18 '12 03:10

user1755011


1 Answers

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 On
RewriteRule ^/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

like image 169
JosefScript Avatar answered Sep 21 '22 15:09

JosefScript