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?
CodePudding user response:
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:
- It will be slow.
- 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.