0

I had to change a template group name for a blog channel from news-and-blog to news-and-forum. I am trying to create a redirect in .htaccess that would redirect the old URL along with any of the blog posts.

The redirect I created works but it is redirecting all of the channel entries to the index page instead of just changing the first segment.

For example, the old URL https://mywebsite.com/news-and-blog correctly redirects to https://mywebsite.com/news-and-forum, but https://mywebsite.com/news-and-blog/my-blog-entry also gets redirected to https://mywebsite.com/news-and-forum. I need it to redirect to https://mywebsite.com/news-and-forum/my-blog-entry

Here is my .htaccess code:

    RewriteEngine On
    
    RewriteRule ^news-and-blog/(.*)$ /news-and-forum/$1 [L,R=301,NC]
    
    # Redirects to https & www
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # Removes index.php
    RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>

I have also tried changing the order of the rewrite rule but always get the same result. And I have tried it with & without the https & www redirect.

Any insight is appreciated.

1 Answer 1

1

There is nothing wrong with your additional RewriteRule - a version of it works just fine on my test system.

You don't say which version of EE you are using, but the latest recommended .htaccess file is a bit different to the one you are using.

The various forms of .htaccess file are documented here; the suggestion is something like this

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    # Removes index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Directs all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Adding in your additional code ends up with something like this:

 <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteRule ^news-and-blog/(.*)$ /news-and-forum/$1 [NC,R=301,L]
    
    # Redirect to https
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # Force use of www prefix
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

    # Remove index.php from ExpressionEngine URLs
    RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
    RewriteCond %{REQUEST_URI} !/system/.* [NC]
    RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

    # Direct all EE web requests through the site index file
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>

Might be worth trying that to see if works any better.

HTH

1
  • 1
    Thank you!!! I updated the code and it now works. Much appreciated!
    – mediapimp
    Apr 26, 2021 at 22:04

Your Answer

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

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