2

I want http://blog.domain.com/ to point to http://www.domain.com/blog

Just a notice, not to redirect to that location, but just point to it.
Also /blog is not a folder. It could be blog.php for example

So when I navigate to http://blog.domain.com, the website display's content from http://www.domain.com/blog

What I tried so far:

server{
  listen 80;
  server_name blog.domain.com;

  rewrite ^/blog(.*) http://blog.domain.com/$1 permanent;    
}

The result is nginx returning 404 not found error.

1
  • Note that adding "permanent" turns this into a redirect.
    – redburn
    Sep 29, 2014 at 11:06

2 Answers 2

1

I try to avoid regex because I'm terrible at it. Perhaps try:

  return 301 http://www.example.com/blog$request_uri;

Pitfalls - Nginx Community

Would setting root to the directory that serves www.example.com/blog perform the function you are looking for?

server{
  listen 80;
  server_name blog.domain.com;
  root /path/to/blog;
}
6
  • 1
    This will redirect the browser, not show the content from the folder on the root of the domain. May 20, 2014 at 0:35
  • Okay, I'm not clear - the question is how to display www.example.com/blog at blog.example.com? Just set index to desired home directory.
    – Paul
    May 20, 2014 at 0:42
  • @MarkHenderson Forgot to mention you in above comment, where I also mistakenly stated index directive when I meant root.
    – Paul
    May 20, 2014 at 1:18
  • Your new answer looks pretty good actually. Better than a rewrite rule. May 20, 2014 at 1:20
  • /blog is a url, not a path. /blog is dynamically generated from within the application logic. Therefore there is no /var/www/domain/blog path.
    – mallix
    May 20, 2014 at 7:08
-1

Your rewrite syntax is the wrong way around. You want to rewrite the root to the subfolder. In that rule you are rewriting the /blog folder, which isn't what you want.

In .htaccess the rule would be:

RewriteRule ^(.*)$ /blog/$1 [L]

Convert that into your nginx format.

4
  • I am using nginx, not Apache.
    – mallix
    May 20, 2014 at 6:44
  • @mallix I know you are. Did you read what I wrote? The last line. Convert that into ngix format. I'm not here to do your job for you, I'm sure a simple conversion should be within your skillset. May 20, 2014 at 7:08
  • Of course it is. I totally missed the last line.
    – mallix
    May 20, 2014 at 7:16
  • In any case, this gives me an nginx not found response.
    – mallix
    May 20, 2014 at 7:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .