简体   繁体   中英

Regex to Remove Query String From .htaccess

I have a blog that I recently migrated from it's original platform into a self-hosted WordPress site. The old platform appended a query string to URLs for mobile views. This doesn't have any connection in the new responsive site, so URLs with those query strings result in 404 errors.

What I need is a regex for my .htaccess that will strip off the query string ?m=1 from a URL. So, for example, "www.example.com/post/?m=1" should rewrite or redirect to "www.example.com/post/"

What I have so far is this:

RewriteCond %{QUERY_STRING}  ^m=1$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]

Which does absolutely nothing :)

Suggestions?

The problem is the trailing slash in /post/?m=1

#stip trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

RewriteCond %{QUERY_STRING}  ^m=1$ [NC]
RewriteRule ^(.*)$ $1? [R=301,L]

My issue was one of syntax. The correct solution is here . Note the importance of placement when using this solution in a WordPress site.

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