0

I've read plenty of Stackoverflows but I seem to be missing something.

I have a PHP application running on https://subdomain.example.com/page/x but for SEO reasons I want people/bots to see https://example.com/subdomain/page/x.

I can rewrite the URL by using:

RewriteEngine on
RewriteCond %{HTTP_HOST} subdomain.example.com
RewriteRule ^(.*)$ https://example.com/subdomain/$1 [L,NC,QSA]

This rewrite results in: https://example.com/subdomain/page/x, but I keep recieving a 404 error since the "main" domain doesn't know the path /subdomain/page/x of course.

What I want is to have the URL https://example.com/subdomain/page/x but run it on https://subdomain.example.com/ in the background since this is the place where the PHP application is running.

Is this possible? How should I do this?

4
  • What is the underlying file structure? Where does the subdomain point to in relation to the domain apex? Are they on the same file system or unrelated? Commented Feb 16, 2022 at 16:19
  • @MrWhite The two are unrelated: on a different server and different technology running. Commented Feb 16, 2022 at 16:21
  • But still Apache? Do you have access to the server config at the domain apex? Commented Feb 16, 2022 at 16:40
  • @MrWhite Not necessarily Apache. There will always be access to the server of "example.com" if that's what you mean. Commented Feb 16, 2022 at 16:48

1 Answer 1

2

There is no strong SEO reason not to use subdomains. See Do subdomains help/hurt SEO? I recommend using subdirectories most of the time but subdomains when they are warranted.

One place where subdomains are warranted is when your content is hosted on a separate server in a separate hosting location. While it is technically possible to serve the content from a subdirectory from the separate server, that comes with its own set of SEO problems:

  1. It will be slow.
  2. It will introduce duplicate content.

From a technical standpoint, you would need to use a reverse proxy to on your example.com webserver to fetch content for the /subdomain/ subdirectory from subdomain.example.com. The code for doing so in the .htaccess file of example.com would be something like:

RewriteEngine on
RewriteRule ^subdomain/(.*)$ https://subdomain.example.com/$1 [P]

The [P] flag means "reverse proxy" which will cause the server to fetch the content from the remote subdomain. This will necessarily make it slower for users. So much so that it would be better for SEO to use a subdomain.

For this to work you would also need to leave the subdomain up and running and serving content for the main server to fetch. This causes duplicate content. You could solve this issue by implementing canonical tags pointing to the subdirectory.

This requires several Apache modules to be available. On my Debian based system I needed to run sudo a2enmod ssl proxy rewrite proxy_connect proxy_http and sudo service apache2 reload. I also had to add SSLProxyEngine on in my <VirtualHost> directive for the site I wanted to use this on.

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

12 Comments

Thanks for your time. I understand what' you're saying concerning SEO and will look into it. I tried your RewriteRule (in the .htaccess of example.com) but it just redirects me to the URL https:/subdomain.example.com/page/x. Which is not my intention since I want the URL https://example.com/subdomain/page/x.
That rule is supposed to proxy it and keep the URL. If it is redirecting, something is wrong.
Any idea what could be wrong? Could it be that the domain.com should be dynamic instead of hardcoded?
How are you testing? Are you using curl --head https://example.com/subdomain/page/x on the command line? Alternately, are you clearing your browser cache between each test? The redirects you are seeing could be coming from browser cache rather than from your server.
If you have just one subdomain you want to proxy, hard coding it is fine. I'd only recommend dynamic if you have a bunch of them and a pattern for their URLs.
|

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.