Quantcast
Channel: Active questions tagged ubuntu - Stack Overflow
Viewing all articles
Browse latest Browse all 6025

301 Redirect blog.example.com to www.example.com/blog using htaccess on Ubuntu/Codeigniter4/Wordpress

$
0
0

I have a blog that is currently hosted on my subdomain and I would like to move it to the /blog subdirectory. The main website uses a CodeIgniter4 framework and the blog uses Wordpress. It is hosted on an Apache server using Ubuntu. I am able to redirect the blog.example.com root to www.example.com/blog, but I am not able to redirect blog.example.com/123 to www.example.com/blog/123

Here is the .htaccess file for the website (the line I added is RewriteCond $1 !^(index\.php|blog), but I included the main part of the file for reference:

# Turning on the rewrite engine is necessary for the following rules and features.# FollowSymLinks must be enabled for this to work.<IfModule mod_rewrite.c>    Options +FollowSymlinks    RewriteEngine On    # If you installed CodeIgniter in a subfolder, you will need to    # change the following line to match the subfolder you need.    # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase    # RewriteBase /    # Redirect Trailing Slashes...    RewriteCond %{REQUEST_FILENAME} !-d    RewriteCond %{REQUEST_URI} (.+)/$    RewriteRule ^ %1 [L,R=301]    # Rewrite "www.example.com -> example.com"    RewriteCond %{HTTPS} !=on    RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]    # Checks to see if the user is attempting to access a valid file,    # such as an image or css document, if this isn't true it sends the    # request to the front controller, index.php    RewriteCond %{REQUEST_FILENAME} !-f    RewriteCond %{REQUEST_FILENAME} !-d    RewriteCond $1 !^(index\.php|blog)    RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]    # Ensure Authorization header is passed along    RewriteCond %{HTTP:Authorization} .    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]</IfModule>

Here is the htaccess for the blog:

# BEGIN WordPress# The directives (lines) between "BEGIN WordPress" and "END WordPress" are# dynamically generated, and should only be modified via WordPress filters.# Any changes to the directives between these markers will be overwritten.<IfModule mod_rewrite.c>RewriteEngine OnRewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]RewriteBase /RewriteRule ^index\.php$ - [L]RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . /index.php [L]</IfModule># END WordPressRewriteCond %{HTTP_HOST} ^blog\.example\.comRewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=301]

And here is the Apache config file for www.example.com:

<VirtualHost *:80>     ServerAdmin me@example.com     ServerName www.example.com     ServerAlias www.example.com     Protocols h2 http/1.1     DocumentRoot /var/www/example-website/public<Directory /var/www/example-website/public>          SetEnv website          Options -Indexes +FollowSymLinks          AllowOverride All          Require all granted</Directory>     Alias /blog "/var/www/example-blog/public_html"<Directory "/var/www/example-blog/public_html">          SetEnv blog          Options -Indexes +FollowSymLinks          AllowOverride All          Require all granted</Directory>     ErrorLog /var/www/example-website/writable/logs/errors.log     CustomLog /var/www/example-website/writable/logs/access.log combined env=website     CustomLog /var/www/example-blog/public_html/wp-content/access.log combined env=blog     RewriteEngine on     RewriteCond %{SERVER_NAME} =www.example.com     RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]</VirtualHost>

For reference, here is the HTTPS config file that gets generated by certbot:

<IfModule mod_ssl.c><VirtualHost *:443>     ServerAdmin me@example.com     ServerName www.example.com     ServerAlias www.example.com     Protocols h2 http/1.1     Alias /blog "/var/www/example-blog/public_html"<Directory "/var/www/example-blog/public_html">          SetEnv blog          Options -Indexes +FollowSymLinks          AllowOverride All          Require all granted</Directory>     DocumentRoot /var/www/example-website/public<Directory /var/www/example-website/public>          Options -Indexes +FollowSymLinks          AllowOverride All          Require all granted</Directory>     ErrorLog /var/www/example-website/writable/logs/errors.log     CustomLog /var/www/example-website/writable/logs/access.log combined env=website     CustomLog /var/www/example-blog/public_html/wp-content/access.log combined env=blog     RewriteEngine on# Some rewrite rules in this file were disabled on your HTTPS site,# because they have the potential to create redirection loops.#      RewriteCond %{SERVER_NAME} =www.example.com#      RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]Include /etc/letsencrypt/options-ssl-apache.confSSLCertificateFile /etc/letsencrypt/live/www.example.com-0001/fullchain.pemSSLCertificateKeyFile /etc/letsencrypt/live/www.example.com-0001/privkey.pem</VirtualHost></IfModule>

Also, related to the config file, is there a way to have a separate ErrorLog for the blog?


Viewing all articles
Browse latest Browse all 6025

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>