Home > Enterprise >  Apache .htacess - 301 redirect doesn't work at all. Using dynamic and friendly URL
Apache .htacess - 301 redirect doesn't work at all. Using dynamic and friendly URL

Time:06-21

I've been trying to create a 301 redirect from a page in my homepage subfolder to another page in the same subfolder. Example:

(1) samedomain.com/homepage/discussions/62-info to (2) samedomain.com/homepage/newinfopost

However, it's not working at all, and I have tried a lot of RewriteRule codes already.

Some relevant information:

  • Both pages are pulled from the database by my software, which is installed in the homepage subfolder
  • Both pages are friendly URLs. My software installed another .htaccess file in homepage to get that done
  • RewriteEngine is on

What I have tried:

  • I have tried disabling friendly URLs temporarily and removing the .htaccess from the subfolder, leaving only the main .htaccess active
  • The .htaccess file (from the main folder) is working. I tested it with different rules and it's fine
  • Some RewriteRules I have tried:

RewriteRule homepage/discussions/62-info/(.*) https://samedomain.com/homepage/newinfopost/$1 [L,R=301]

RewriteRule homepage/discussions/62-info/(.*) /homepage/newinfopost/$1 [L,R=301]

RewriteRule /homepage/discussions/62-info/$ /homepage/newinfopost/$1 [L,R=301]

RewriteRule ^/?homepage/discussions/62-info/(.*)$ https://samedomain.com/homepage/newinfopost/$1 [L,R=301]

The .htaccess in the subfolder (that is managed by my software to transform URLs into friendly ones) is this:

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map|webp)(\?|$) /homepage/404error.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /homepage/index.php [L]
</IfModule>

But like I said, I have tried removing that file and disabling friendly URLs too.

Edit:

I believe my pages are dynamic. When they're not friendly URLs, they look like this:

https://samedomain.com/homepage/index.php?app=discussions&id=62

Test 1 based on @MrWhite's answer:

RewriteRule ^discussions/62-info$ /homepage/newinfopost [R=302,L]

<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpeg|jpg|gif|png|ico|map|webp)(\?|$) /homepage/404error.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /homepage/index.php [L]
</IfModule>

CodePudding user response:

To redirect /homepage/discussions/62-info to /homepage/newinfopost then you should add the following to the very top of the /homepage/.htaccess file:

RewriteRule ^discussions/62-info$ /homepage/newinfopost [R=302,L]

The RewriteRule pattern (1st argument) matches a URL-path relative to the directory that contains the .htaccess file. So, this should not contain homepage/ when in the /homepage/.htaccess file.

Note that in .htaccess the URL-path that is matched does not start with a slash.

When the /homepage/.htaccess file is present (contain mod_rewrite directives) then any mod_rewrite directives in the parent config (ie. in /.htaccess) are completely overridden - they are not even processed (by default).

Always test first with a 302 (temporary) redirect to avoid potential caching issues. You should clear your browser cache before testing.


RewriteRule homepage/discussions/62-info/(.*) https://samedomain.com/homepage/newinfopost/$1 [L,R=301]

RewriteRule homepage/discussions/62-info/(.*) /homepage/newinfopost/$1 [L,R=301]

RewriteRule /homepage/discussions/62-info/$ /homepage/newinfopost/$1 [L,R=301]

RewriteRule ^/?homepage/discussions/62-info/(.*)$ https://samedomain.com/homepage/newinfopost/$1 [L,R=301]

None of these would match the example URL, since the example does not include a trailing slash. The 3rd rule also has an erroneous slash prefix. (But why do you have a capturing subpattern and $1 backreference? There doesn't appear to be anything that needs "capturing" in your example URL?)

It would also depend on where (and which file) these directives are being used.


Aside:

RewriteBase /home/

In the directives you've posted the RewriteBase directive is not being used. However, it would seem to be set incorrectly, as the base URL would seem to be /homepage, not /home.

  • Related