Page 1 of 1

Pretty URL .htaccess rewrite subdomain to directory

Posted: Wed Sep 25, 2019 5:59 pm
by magallo
hi guys, i need help figuring out how to .htaccess rewrite subdomain to directory and keep subdomain in url

For example, i need the result of this url:
https://mywebsite.com/reviews/name

to appear like this:
https://reviews.mywebsite.com/name

The content of https://mywebsite.com/reviews/name is generated by LISE and works as expected using the default .htaccess provided by cmsms.

Code: Select all

<IfModule rewrite_module>

Thanks in advance.

	RewriteEngine on
	# If your CMSMS installation is in a subdirectory of your domain, you need to specify the relative path (from the root of the domain) here.
	# In example: RewriteBase /[subdirectory name]
	RewriteBase /
    
    # +++++ ADD THIS - START +++++
      # Link to http(s)://www.website.com then redirect to https://website.com
      RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
      RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

      # Force URLs have https instead of http
      RewriteCond %{HTTPS} off
      RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    # ++++++ ADD THIS - END +++++
	
	# Rewrites URLs in the form of /parent/child/grandchild but only rewrites if the requested URL is not a file or directory.
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.+)$ index.php?page=$1 [QSA]
</IfModule>

Re: Pretty URL .htaccess rewrite subdomain to directory

Posted: Thu Sep 26, 2019 6:51 am
by velden
First of all .htaccess rewrite rules are typically used to rewrite request urls, not to generate urls in webpages.

The generation of the url should be done from within the LISE template probably.

I think the rewriting back (from https://reviews.mywebsite.com/name to https://mywebsite.com/reviews/name) may be done within the .htaccess rewrite rules. But probably depends on the configuration of your web environment. If you actually serving the sub domain from a separate folder, you may be able to do it by adding the .htaccess file with proper rules there.
If the subdomain 'points' to the webroot (where CMSMS is installed) you should able to use the .htaccess from that folder to rewrite the url.

Re: Pretty URL .htaccess rewrite subdomain to directory

Posted: Thu Sep 26, 2019 7:00 am
by calguy1000
The ,.htaccess file ONLY handles incoming requests, it does not transform URLS the output HTML generated by the web application.

Re: Pretty URL .htaccess rewrite subdomain to directory

Posted: Thu Sep 26, 2019 7:37 pm
by magallo
I ended up pointing the subdomain to the same folder where the main site is. I then edited the LISE item url to only have:
name
instead of
reviews/name

It works but i need to make sure search engine doesn't see duplicate content.

Re: Pretty URL .htaccess rewrite subdomain to directory

Posted: Sat Sep 28, 2019 5:36 pm
by Dr.CSS
You should be able to add a subdomain in your cPanel which will give you that URL no need for htaccess except maybe for pretty URLs and...

#Sub-dir e.g: /cmsms
RewriteBase /reviews

I how I do all mine...