1

I created a Digital Ocean instance to install WordPress website 5 days ago. I already have my own domain, and created DNS Subdomain blog.example.com. The server runs both nginx and apache so my Apache port is 8082. Below is what my actual WordPress URL looks like:

www.example.com:8082/wordpress/

Something which I have tried is below.

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com:8082/wordpress\
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.example.com [R=301,NC,L]

</IfModule>

Now my expected result is from

www.example.com:8082/wordpress\ 

to

blog.example.com
  • Where is your .htaccess file located? In the document root, or in the /wordpress subdirectory? – MrWhite Jul 18 at 13:23
0
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com:8082/wordpress\

The HTTP_HOST server variable contains the value of the Host HTTP request header - this does not include the URL-path (ie. /wordpress/). But why have you also include a backslash (\) at the end, instead of a (forward) slash (/)? Is that just a typo? (Although you've also carried that through to the example below?)

RewriteRule ([a-z0-9-]+)/? http://$1.example.com [R=301,NC,L]

Not quite sure what the idea behind this is, but this directive assumes that blog is present in the requested URL-path - but it's not in your example? And, unless you have multiple subdomain redirects, there's seemingly no need for the generalised match and substitution?

I would also question why you need the the conditions that check that the request does not map to a directory or a file. By checking that it does not map to a directory, it won't redirect /wordpress/ (assuming this is a physical directory).

To redirect www.example.com:8082/wordpress/ to blog.example.com as in your example (a single URL redirect*1) then you would need to do something like the following instead, at the top of your root .htaccess file:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com:8082
RewriteRule ^wordpress/$ http://blog.example.com/ [R=301,NC,L]

If the .htaccess file is located inside the /wordpress subdirectory then change the RewriteRule pattern to read ^$ instead of ^wordpress/$

If example.com:8082 and blog.example.com point to different areas of the filesystem then you can remove the condition (RewriteCond directive) altogether.

No need for the <IfModule> wrapper or RewriteBase directives here.

*1 Although I'm surprised you don't want to mass redirect all sub-URLs as well? ie. Redirect www.example.com:8082/wordpress/<something> to blog.mydomain.com/<something>. Or do you?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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