skip to Main Content

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&param2=b

DESIRED output: https://www.example.com/mypage?param1=a&param2=b

Example 2:

Input: https://www.example.com/2019/12/mypage.html?param1=a&m=1&param2=b

DESIRED output: https://www.example.com/2019/12/mypage?param1=a&param2=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


  1. Could you please try following, written as per shown samples. Also please make sure you clear your browser cache before testing your URLs.

    RewriteEngine ON
    # Static pages: Remove /p from path and remove .html
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^p(/.*?)(?:.html)?$ $1 [R=301,L]
    
    # All other posts just remove .html
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{REQUEST_URI} ^/(.+).html?$ [NC]
    RewriteRule ^ %1 [L,R=301]
    
    #Check if URI has .html with query string present in it.
    RewriteCond %{THE_REQUEST} s(?:/p)?/(.*).html?(.*)(?:[&?])m=1(.*)s [NC]
    RewriteRule ^ %1?%2%3 [R=301,L]
    


    2nd solution: In case you are looking for specifically only param1 and param2 parameters then try following.

    RewriteEngine ON
    # Static pages: Remove /p from path and remove .html
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^p(/.*?)(?:.html)?$ $1 [R=301,L]
    
    # All other posts just remove .html
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{REQUEST_URI} ^/(.+).html?$ [NC]
    RewriteRule ^ %1 [L,R=301]
    
    #Check if URI has .html with query string present in it.
    RewriteCond %{THE_REQUEST} s(?:/p)?/(.*).html?(param1=[^&]*)(?:[?&])m=1(param2=[^s]*)s [NC]
    RewriteRule ^ %1?%2%3 [R=301,L]
    
    Login or Signup to reply.
  2. You can use the following rule to remove m=1 query perameter from your URLs.

    RewriteEngine on
    RewriteCond %{QUERY_STRING} (.*?)&?(?:bm=1)(.*)$
    RewriteRule ^ %{REQUEST_URI}?%1%2 [R,L]
    
    Login or Signup to reply.
  3. You may use this rule to remove a query parameter if present and remove /p or .html as well.

    Here is your complete .htaccess:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} ?(.*&)?m=1&?(S*)sHTTP [NC]
    RewriteRule ^(?:p/)?(.+)(?:.html)?$ /$1?%1%2 [R=301,NE,NC,L]
    
    # Static pages: Remove /p from path and remove .html
    RewriteRule ^p(/.*?)(?:.html)?$ $1 [R=301,L,NC,NE]
    
    # All other posts just remove .html
    RewriteRule ^(.+).html$ /$1 [L,R=301,NC,NE]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search