1

I hope that I'll explain myself in my needs. I'm trying to write a dynamic htaccess but I don't achive to understand the conditions and the rewriting rules.

Here is my thing, I built a small CMS where the user will write their base url like "stackoverflow.com" and he'll choose with some radio input between http or https and also between www or not. Also for SEO porposes, one thing that it's important is to force the slash except in url with extensions.

So, the options for ssl and www are stored in DB, so I wanted to know what do you think, if it's better to write some code like:

if($ssl){
  $htaccess.= "RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1/ [R=301]\n";
}else{
  $htaccess.= "RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1/ [R=301]\n";
}

As you can see that works if I want to add the www, so I was trying to add more php conditions but i'm sure there must be a way to do it in just a few htacess lines, I already see that we can use conditions like this:

RewriteCond %{HTTP_HOST} ^www.stackoverflow.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s) 
RewriteRule ^ http%1://www.stackoverflow.com:2368%{REQUEST_URI} [P,QSA,L]

But again all this codes I found have a fixed www or non-www and I don't know if they keep the slash. So, I want to know if I can add more conditions to the code above to have the www as variable to, and to ask if there's some extention to erase the slash. Something like:

RewriteCond %{HTTP_HOST} ^www.stackoverflow.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteCond %{HTTP_HOST} !^www. [OR]
RewriteCond %{HTTPS}:s on:(s)
RewriteRule ^ http%1://%2stackoverflow.com%{REQUEST_URI} [P,QSA,L]

I wrote this but I don't know if I can keep the variable (s) with the same name, or if %2 take the value, or if there's a limit of htaccess conditios. Because I was trying to found some htaccess docs for this but I don't get it, and everything I try make show some wrong configuration screen so I hope that you can at least give some advices about how to use this variables or if you think If there's and easy way to do it.

Thanks in advance.

0 Answers0