1

I need some extra code in my .htaccess file that configures this HTTP request for HTTPS too!

<ifmodule mod_rewrite.c>

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

Options +FollowSymlinks
</ifmodule>

My problem: now it gets the /sponsor tag only if I type the example with http:

GOOD:

  • This is working: http://subdomain.example.com results in http://subdomain.example.com/sponsor/subdomain.

BAD:

  • This isn't working: https://subdomain.example.com results in https://subdomain.example.com/.

I need a .htaccess rewrite, that automatically puts the /sponsor TAG into the HTTPS URLs too.

UPDATE: Here is my .htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [OR,NC]
RewriteCond https://%{HTTPS_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/sponsor/%1/$1 [L,NC,QSA]

Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
4
  • The code you posted is not doing this. Please post your entire .htaccess file. The code you posted would result in a "redirect loop" - so there would seem to be something else going on here? It also does not differentiate between HTTP and HTTPS, so again, something else is going on here if you are seeing a different response. The code you posted (even if it did "work") would not result in the "GOOD" redirect you've stated - so, yet again, something else is going on here.
    – MrWhite
    Commented Jan 20, 2019 at 17:22
  • Here is more for you: # BEGIN WordPress <IfModule mod_rewrite.c> Options +FollowSymlinks RewriteEngine On RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [OR,NC] RewriteCond https://%{HTTPS_HOST} ^(.*)\.example\.com [NC] RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/sponsor/%1/$1 [L,NC,QSA] Options +FollowSymlinks RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] </IfModule> # END WordPress Commented Jan 20, 2019 at 18:50
  • I just need the /sponsor/ current subdomain for every TYPED URL in the browser. For http:// and for https:// url's too. Commented Jan 20, 2019 at 18:53
  • I've edited your question and added your code dump. Apart from "looking a mess", when you post unformatted code it is likely that certain characters get omitted from display, particularly in regex - so please check my edit for any copy/paste errors.
    – MrWhite
    Commented Jan 20, 2019 at 21:10

1 Answer 1

0
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [OR,NC]
RewriteCond https://%{HTTPS_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}/sponsor/%1/$1 [L,NC,QSA]

Slightly different to the directives you posted initially, however, the second condition will never match, so the result is the same.

But as stated in my comment, this will result in a "redirect loop" and could not possibly result in either of the redirect responses you state in the question ("good" or "bad"). So, either you are seeing a cached response or .htaccess (or mod_rewrite) is not enabled and WordPress itself is performing this redirect?! (Although presumably, you do have WordPress "pretty" URLs enabled?)

Note that your RewriteRule substitution string is duplicating the URL-path, both at the front of the URL-path (%{REQUEST_URI}) and at the end (with the $1 backreference) - so this would also break on anything other than the document root (which is all you seem to be testing in your question).

Also, you should never add your own directives to the WordPress code block - otherwise WordPress will "try" to overwrite your code when it updates.

You should add your own directives before the # BEGIN WordPress section.

To perform the required redirect you would need something like the following instead:

RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com [NC]
RewriteRule !^sponsor/ https://%{HTTP_HOST}/sponsor/%1%{REQUEST_URI} [R,L]

UPDATE: Remove the trailing $ on the RewriteRule pattern. ie. What was !^sponsor/$ should be !^sponsor/ (as above).

Note that the above will also catch the www subdomain, if that is enabled. If you only have the one domain (ie. example.com) then you can simplify the condition to read:

RewriteCond %{HTTP_HOST} ^([^.]+)\.

In summary, with all code in place:

Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^([^.]+)\.
RewriteRule !^sponsor/ https://%{HTTP_HOST}/sponsor/%1%{REQUEST_URI} [R,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

There is no need to repeat the RewriteEngine directive.

Make sure you clear your browser cache before testing.

6
  • Hi MrWhite, I am very.very thank you this suggestion. Thanks for the tipps too. It seems very close to my success. Now the code works with http:// and with https:// too, but the url gives back a continuosly /sponsor/example/sponsor/example/sponsor/example TAG. When i open HTTP subdomain.example.com or HTTPS subdomain.example.com i get subdomain.example.com/sponsor/example/sponsor/example/sponsor/… I think missing some closing tag. Commented Jan 21, 2019 at 9:34
  • Ah yes, sorry, there was an error in the regex.... Remove the trailing $ on the RewriteRule pattern. ie. What was !^sponsor/$ should be !^sponsor/ - I've updated my answer.
    – MrWhite
    Commented Jan 21, 2019 at 9:40
  • 1 last question: Can we remove the default adding "index.php" from the end of the URL? Commented Jan 21, 2019 at 10:10
  • That's a very different question. It's not clear why "index.php" would be added in the first place (unless you don't have WordPress "pretty" permalinks enabled)? It's not something that these directives are doing.
    – MrWhite
    Commented Jan 21, 2019 at 10:17
  • Yes, the pretty permalinks are enabled. Now all the URL's get at the end the index.php tag. It would be better for me this way simple: subdomain.example.com/sponsor/example Commented Jan 21, 2019 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.