I’m fairly new to .htaccess, but have gotten close to what I need, but am now stuck.
Input: https://www.example.com/p/mypage.html
Output: https://www.example.com/mypage
This is achieved with the following:
# Static pages: Remove /p from path and remove .html
RewriteRule ^p(/.*?)(?:.html)?$ $1 [R=301,L]
# All other posts just remove .html
RewriteCond %{REQUEST_URI} ^/(.+).html?$ [NC]
RewriteRule ^ /%1 [L,R=301]
However, very often there is a query parameter m=1 that needs removed IF IT IS PRESENT
I need that query parameter removed for both static pages AND other posts in the above RewriteRules. Note that there very often may be other query parameters also present but that I do not want those to be removed.
Example 1:
Input: https://www.example.com/p/mypage.html?param1=a&m=1¶m2=b
DESIRED output: https://www.example.com/mypage?param1=a¶m2=b
Example 2:
Input: https://www.example.com/2019/12/mypage.html?param1=a&m=1¶m2=b
DESIRED output: https://www.example.com/2019/12/mypage?param1=a¶m2=b
In both of the above examples, all of the following are removed:
-
/p
if present -
.html
if present -
m=1
if present (sometimes it may be the only query parameter so that also needs taken into account)
(By the way, the above three bullets will be useful for anyone moving a site over from Blogger / Blogspot to WordPress, as they represent the differences in how the paths and pages are handled)
Very much appreciate any help with the m=1
being removed if present.
3
Answers
Could you please try following, written as per shown samples. Also please make sure you clear your browser cache before testing your URLs.
2nd solution: In case you are looking for specifically only param1 and param2 parameters then try following.
You can use the following rule to remove
m=1
query perameter from your URLs.You may use this rule to remove a query parameter if present and remove
/p
or.html
as well.Here is your complete .htaccess: