This question already has answers here:
301 Redirect to remove query string on homepage only
(1 answer)
Rewrite from any query string after root to root in .htaccess
(2 answers)
Closed 10 months ago.
I am using parameters on some subpages, but google index them in the search results for some reason in the homepage URL.
How can I please remove them in .htaccess only for homepage?
remove ?a=123 here:
example.com/?a=123 (result: example.com)
don't remove it here:
example.com/subpage/event.php?a=123
I found this, but I don't know how to apply it only for homepage:
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) $1?
Use this:
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule ^/?$ / [R=301,L,QSD]
%{QUERY_STRING} . means "query string contains at least one character"
^/?$ is "just the home page" -- ie the URL path only consists of an optional slash
[R=301] Redirects the request (to change the URL)
[L] is the last rewrite rule so that it doesn't conflict with later ones
[QSD] is "query string delete" so that rewrite doesn't try to append the query string to the redirect
Related
I have a hacked website where a hacker has injected e-commerce pages.
The pages are ranked on google but I want to redirect them to the homepage.
They have the following URL structure. The digits are randomly generated and they are almost 1000 on SERP. I have put random digits as an example:
https://example.com/?itemywdy11111111.html
https://example.com/?itemywdy51111.html
Below is my htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} ^itemywdy$
RewriteRule (.*) / [QSD,R=302,L]
The code above is not working at all.
How do I do a 301 redict using htaccess for these urls.
You should match only the starting part of query string leaving end anchor:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)itemywdy [NC]
RewriteRule ^ / [QSD,R=301,L]
Also note use of (?:^|&) to let this string match anywhere in a query string which would allow this rule to work for your existing pages with other query strings as well.
I have several URLs with question marks that need to be removed. For example, I need to redirect this URL:
http://example.net/?/services
To this URL:
http://example.net/services
I have many more like this, so I would like something that can catch everything with the question mark and properly redirect it. Many of the answers I found were trying to use QUERY_STRING as the condition for the rewrite, but without parameters this does not help. After some digging I found a RewiteCond that works, but the RewriteRule redirects to the homepage, rather than the URL without the question mark. What I have currently is this:
# Remove question mark from string
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(index\.php)?\?([^&\ ]+)
RewriteRule ^ /%1? [L,R=301]
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
The first block is the rewrite that I have so far, and the next two are for the CMS url routing. What seems to be happening is that my rewrite in the first block is not keeping the rest of the url. I have tried several combinations and can't seem to figure out how to keep the rest of the url intact.
Many of the answers I found were trying to use QUERY_STRING as the condition for the rewrite, but without parameters this does not help.
Yes, this is exactly what the first URL, with a question mark, contains. So, I'm not sure why "this does not help"? In the URL http://example.net/?/services, /services is the query string. Whether there are key/value pairs (ie. "parameters") is irrelevant.
To redirect URLs of the form http://example.net/?/services, that consist of no URL-path and only a query string, try something like:
# Remove question mark from string
RewriteCond %{QUERY_STRING} ^/(.+)
RewriteRule ^$ /%1? [R,L]
%1 is a backreference to the captured group in the last matched CondPattern (ie. (.+), which captures services). This assumes that the query string (after the ?) always starts with a slash, as in your example. (Incidentally, this is also what your front controller is doing, in reverse, so I assume it must be correct.)
The trailing ? on the substitution removes the original query string from the request.
Make sure you clear your browser cache, as any earlier/erroneous 301s will have been cached by the browser.
If this is intended to be a permanent (301) redirect then change R to R=301, but only when you are sure it's working OK.
I'm working with mod_rewrite under .htaccess, and I'm trying to redirect (R=301) an URL like this :
http://domain/index.php?folder=AB_CD
to an URL like this
http://domain/AB/CD/
How can I write the rule please ?
Try the following code in root/.htaccess :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
Explaination :
RewriteCond %{QUERY_STRING} ^folder=([^_]+)_([^&]+)$ [NC]
Checks to ensure that the url (index.php) has query strings with specific key and value, ( folder=foo_bar) acording to the regex pattern, if the url has valid query strings then the rule is processed
RewriteRule ^index\.php$ http://domain.com/%1/%2/? [NC,L,R]
index.php?query_strings gets redirected to /query/strings, if the condition is met.
Empty question mark ? at the end of the Rewrite target is important as it discards the orignal query strings, without it /index.php?folder=foo_bar redirects to /foo/bar/?folder=foo_bar appending the old query strings.
(Hope, this helps!)
This question already has answers here:
Rewriting URL with selected query string parameters in .htaccess
(3 answers)
Closed 8 years ago.
How do change:
domain.nl/index.php?cat=123&page=456&title=789
to
domain.nl/?p=456
I think I need to change .htaccess and use rewrite rules, but I can't get it to work.
I would like to use the value in variable page in the new URL. Thanks!
This is only possible if index.php doesn't need the information in the cat and title variables, since you remove this information from the url. I am also assuming that index.php uses the query variable, and not the THE_REQUEST variable to get this information. You need two rules. One to externally redirect the old url to the new url. The other one needs to only work on external requests that match the url, and internally rewrites it to the old url that works.
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /\?p=([^&]+)
RewriteRule ^ /index.php?page=%1 [L]
RewriteCond %{QUERY_STRING} page=([^&]+)
RewriteRule ^index\.php$ /?p=%1 [R,L]
first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.