Home > database >  How to redirect an URL with many parameters to a subfolder name created from the first parameter, wi
How to redirect an URL with many parameters to a subfolder name created from the first parameter, wi

Time:12-16

How to redirect from:

https://example.com/blog/?p=title-of-blog-post&utm_source=browser&utm_medium=rss_notification&utm_id=600215.0720246513

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
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]

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

Only the first parameter is of interest.

Because of the rest of the parameters, namely: &utm_source=browser&utm_medium=rss_notification&utm_id=600215.0720246513 , I now receive the 404 error.

Please help me!

CodePudding user response:

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

Your first rule already does what you require when p is the only URL parameter. To handle additional URL params then just remove the trailing \s (that matches a space) at the end of the CondPattern.

For example:

RewriteCond %{THE_REQUEST} \s/blog/\?p=([\w-] ) [NC]
:
  • Related