简体   繁体   中英

How to redirect an URL with many parameters to a subfolder name created from the first parameter, with .htaccess?

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM