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.
Related
I have a site at www.test.com. The site has many nested directories and categories such as www.test.com/cat/1/321/.
I want to use a Regex to remove any url ending in /321/ or /321 to the previous category.
Example: Redirect www.test.com/cat/1/321/ to www.test.com/cat/1/
Needed for a Wordpress site using .htaccess. Thanks!
You can write your conditions and rules for exam in this code we have to conditions for /321/ and /321
RewriteCond %{REQUEST_URI} /321/$ [NC]
RewriteRule ^(.*)/321/$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} /321$ [NC]
RewriteRule ^(.*)/321$ /$1 [R=301,L]
Simple enough
RewriteEngine On
RewriteRule ^(.*/)321/?$ $1 [R,L]
I used to have a query string for handling mobile pages. Now the site is responsive it doesn't need it and I need to do 301 redirects let google know.
so
http://www.example.com/aaa/bbb?fromMobile=true
should become
http://www.example.com/aaa/bbb
and
http://www.example.com/aaa?fromMobile=true
should become
http://www.example.com/bbb
and
http://www.example.com?fromMobile=true
should become
http://www.example.com
etc
I have:
RewriteCond %{QUERY_STRING} ^(?)fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
But this redirects:
http://www.example.com?fromMobile=true
to
http://www.example.com?
How can I get rid of the trailing question mark? I've tried a bunch of things without joy.
You can use:
RewriteCond %{QUERY_STRING} ^fromMobile=true$ [NC]
RewriteRule (.*) /$1? [R=301,L]
This is assuming there is no other query parameter besides fromMobile=true.
If there is a chance of other query parameters also then use:
RewriteCond %{QUERY_STRING} ^(.+?&)?fromMobile=true(?:&(.*))?$ [NC]
RewriteRule (.*) /$1?%1%2 [R=301,L]
I'm stuck trying to redirect my old posts on my wordpress blog from www.domain.com/?p=444 to just the root, this is because the posts have been removed.
This is what i have tried so far.
RewriteCond %{QUERY_STRING} p=[0-9]+
RewriteRule ^p$ / [L,R=301]
Any help would be much aprreciated.
Best Regards
Use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} p=[0-9]+ [NC]
RewriteRule ^$ /? [L,R=301]
/? means root URL with ? for removing existing query string removed.
Sorry for duplicating the thread (htaccess rewrite URL to append Google campaign tracking causing redirect loop) but the other one requires 50 points to post a comment which I don't have
I have this simple alias in my .htaccess
RewriteCond %{REQUEST_URI} /contact$
RewriteRule .* /content.php?id=17 [L]
and I'm using analytics tracking so need to add a QS like
?utm_source=somesource&utm_medium=email&utm_campaign=somecode
to my alias.
I tried change it in .htaccess to the following but neither of them work:
RewriteCond %{REQUEST_URI} /contact$
RewriteRule .* /content.php?id=17 [L,QSA]
RewriteCond %{REQUEST_URI} /contact$
RewriteRule .* /content.php?id=17&%{QUERY_STRING} [L]
How to catch the query string then?
Thank you
Replace all of your code with this:
RewriteRule ^contact/?$ content.php?id=17 [L,QSA,NC]
I'm trying to redirect old-style links from an old website to new style links in php.
I'm using:
RewriteEngine on
RewriteBase /
RewriteRule s\.cfm?id=5$ http://mysite.com/article5.php [B,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
However, if I use just s.cfm? everything works fine and redirects to article5.php. But if I try to redirect the id=5 part, I get page not found.
I tried just s.cfm?id and it causes the htaccess bug. So anything you put after question mark ?... causes a problem, I don't know why.
You can't match against the query string inside a RewriteRule, only the URI path is sent through the rule itself. If you need to match against the query string, then use the %{QUERY_STRING} var inside a RewriteCond:
RewriteCond %{QUERY_STRING} ^id=5$
RewriteRule ^/?s\.cfm$ http://mysite.com/article5.php [L,R=301]
Everything else is fine.