10

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!

0

4 Answers 4

5
+100

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

Sign up to request clarification or add additional context in comments.

1 Comment

would it be possible to redirect from: yoursite.com/something to yoursite.com?content=something ?
3

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

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

1 Comment

This would catch the server name in the first group which is not necessary for a rewrite.
3

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

RewriteEngine On
RewriteCond %{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; 
}

1 Comment

There is no need for two steps. If you catch "location" with the regex, direct requests to "subdirectory" will always pass.
2

This would probably be the best way to do it in nginx:

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

For more details, see:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.