Finder
Finder

Reputation: 55

How to redirect a parameter to a subfolder with .htaccess?

How to redirect from:

https://example.com/blog/?p=title-of-blog-post

to:

https://example.com/blog/title-of-blog-post

In the /blog/ folder, I have the following .htaccess file:

Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteRule ^.*$ ./index.php
</IfModule>

I have tried into the .htaccess of the root, the following code, but it returs 404 Not found:

.htaccess in /public_html/

RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/blog/\?p=([0-9]*)\s [NC]
RewriteRule . blog/%1? [R=301,L]
RewriteRule ^blog/([0-9]+)$ blog/?p=$1 [L]

Upvotes: 1

Views: 102

Answers (1)

anubhava
anubhava

Reputation: 784958

You must use new rule in blog/.htaccess only. Here is suggested code:

Options +FollowSymLinks +ExecCGI
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/

RewriteCond %{THE_REQUEST} \s/blog/\?p=([\w-]+)\s [NC]
RewriteRule ^ %1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ ?p=$1 [L,QSA]

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

Upvotes: 1

Related Questions