I've got a buggy query string in my url so it appears in the beginning of a url:
https://website.com/?lang=en/wp-content/uploads/2019/10/image.png
How can I remove this query string so that the url redirects to https://website.com/wp-content/uploads/2019/10/image.png?
I've tried the following rules to no avail:
RewriteEngine On
RewriteCond %{QUERY_STRING} "lang=" [NC]
RewriteRule ^(.*) /$1? [R=301,L]
Any help appreciated. Thank you!
The path component of your URL is empty here, so RewriteRule ^(.*) will only capture an empty string, and that means $1 will be empty as well.
The info you are looking for is in the query string - so you have to capture it from there:
RewriteEngine On
RewriteCond %{QUERY_STRING} lang=(.*) [NC]
RewriteRule . /%1? [R=301,L]
%1 instead of $1, because that is a back reference not to the RewriteRule pattern, but to the Condition.
Now this might lead to unexpected results if your query string could ever contain more GET parameters after lang. In that case, you might have to be a bit more specific with your pattern (like try to match anything after lang, that is not an ampersand - lang=([^&]*))
Related
I would like to redirect the following urls from:
http://example.com/index.php/item123-detail?tmpl=component&format=pdf
to:
http://example.com/index.php/item123-detail
In essence removing the "?tmpl=component&format=pdf" from all urls.
I have tried multiple different examples from other Stack questions without luck so far. Any help would be much appreciated. Thank you.
This part of URL ?tmpl=component&format=pdf called QUERY_STRING and if you want to remove it from any request you could do several scenarios like putting this code at main directory .htaccess this :
RewriteEngine On
RewriteCond %{THE_REQUEST} \?
RewriteRule ^(.*)$ /$1? [L,R=301]
So , by the code above you will be able to remove even request with ? only.
If you want to match only this query string and keep others , let me know to give you another scenario with another condition RewriteCond %{QUERY_STRING}
Ok , to match only this query string , replace the code with :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tmpl=component&format=pdf$
RewriteRule ^(.*)$ /$1? [L,R=301]
And if tmpl & format values not fixed and come only into letters, replace the code with:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tmpl=([a-zA-Z]+)&format=([a-zA-Z]+)$
RewriteRule ^(.*)$ /$1? [L,R=301]
Solution found on another post as follows:
remove query string from end of url URL using .htaccess
RewriteCond %{QUERY_STRING} "post_type=" [NC]
RewriteRule (.*) /$1? [R=301,L]
So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]
I am having problems understanding the rewriting rules. Basically what I am trying to do is change this:
http://domain.com/Intern/guangjian/gallery/?page=admin_login
Into this:
http://domain.com/Intern/guangjian/gallery/admin_login
I keep getting 500 Internal Server Error when I applied various rules to my .htaccess file. Please help. Currently I have the below:
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^(\w+)=(\w+)$
RewriteRule ^/%2
I admit I don't really understand what all the symbols mean.
The symbols are converting the characters into a sequence of bytes in a specific set using character encoding
Example
Original URL.
Eg. http://flickr.com/users.php?id=username&page=2
Rewriting Friendly URL.
Eg. http://flickr.com/username/2
//First Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?user=$1
//Second Parameter
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ users.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ users.php?user=$1&page=$2
Place this code in /Intern/guangjian/gallery/.htaccess file:
RewriteEngine On
RewriteBase /Intern/guangjian/gallery/
RewriteCond %{THE_REQUEST} /\?page=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([^/.]+)/?$ ?page=$1 [L,QSA]
Rewrite Rule syntax is: RewriteRule Pattern Substitution [flags].
This means only the flags (L, NC, R, etc...) are optional, the pattern to match and the substitution parts are mandatory. But you only specified one, which is why you are getting a 500 Internal Server Error.
You don't need RewriteCond. Try this:
RewriteEngine ON
RewriteRule ^(.*)\?(\w+)=(\w+)$ $1/$3
You are getting everything up to the ?, and then attaching the value of page, or whatever argument of the query string, to the url. This of course only works when you have only one argument in the query string, the regex won't match in other case.
I am dealing with an API that is accessing an URL on my website and adding
/?parameter1=value¶meter2=value to the url. I want my htaccess to handle this request and only keep the parameter's values. The API is also adding many other parameters in the query string but I just want to keep two of them.
The following does not work:
RewriteCond %{QUERY_STRING} ^parameter1=([^&]+)
RewriteCond %{QUERY_STRING} ^parameter2=([^&]+)
RewriteRule ^my-url/?$ controller.php?parameter1=%1¶meter2=%2
How can I do that correctly?
EDIT:
Here is an example.
The url is:
http://example.com/my-url/?parameter1=value1&stuff=1&stuff2=2¶meter2=value2
The htaccess should get the parameter1 & parameter2 values.
Try this and just grab the two parameters in your server side code. e.g. $_GET. If they are always in the query string, you can just check for parameter1 and then it will append the other parameters and you can get what you need.
RewriteCond %{QUERY_STRING} ^\bparameter1=
RewriteRule ^my-url/?$ controller.php [QSA,L]
Try the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(GET|POST)\ /my-url/\?parameter1=(.*)&stuff=(.*)&stuff2=(.*)¶meter2=(.*)\ HTTP
RewriteRule ^ /controller.php?parameter1=%2¶meter2=%5\? [R,L]
Mind it will only get the two parameters if there are always &stuff=1&stuff2=2 those two values in the url.
I've had to do a similar setup with key/value pairs in the GET which could come in any order depending on the client platform.
Based on useful pointers in this thread, this is what I came up with based on the question above:
#= 1. Catch parameter1, append querystring as "p1=". Rewrite to "/parameter1" below:
RewriteCond %{REQUEST_URI} ^/my-url/$
RewriteCond %{QUERY_STRING} ^parameter1=([^&]+)
RewriteRule (.*) /parameter1?%{QUERY_STRING}&p1=%1 [P,QSD]
#= 2. Catch parameter2, append querystring as "p2=". Rewrite to "/parameter2" below:
RewriteCond %{REQUEST_URI} ^/parameter1$
RewriteCond %{QUERY_STRING} ^parameter2=([^&]+)
RewriteRule (.*) /parameter2?%{QUERY_STRING}&p2=%1 [P,QSD]
#= 3. Now explicitly catch p1,p2 and feed to them to the back-end url:
RewriteCond %{REQUEST_URI} ^/parameter2$
RewriteCond %{QUERY_STRING} p1\=([^&]+)&p2\=([^&]+)$
RewriteRule (.*) controller.php?parameter1=%1¶meter2=%2 [P,L,QSD]
The above works fine for me but I do notice it a tiny bit slow to process, so if anyone has any criticism then I would be glad to hear it!
My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here's what I thought should remove % on the query string and replace with A but it's not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can't quite spot it. Thanks for the expert eyes!
You're real close.
The problem here is that you've got a condition and the match of your rule should be together. Your backreference to the previous RewriteCond is broken because it's for the REQUEST_URI and not the QUERY_STRING like you want.
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
Here, the %1 backreference matches the (.*) at the end of the /SpecialPage URI. The backreferences from your query string match gets lost, and that's the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteRule ^SpecialPage(.*)$ /SpecialPage$1?%1A%2 [L]
Here, the %1 and %2 backreferences correctly reference the query string and the SpecialPage condition in the URI is met by the regex pattern.
Redirect Query String
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)=(.)$
RewriteRule ^(.)?(.)$ /$1--%0? [R=301,L]
From Url: http://localhost/sholay-slide.jsp?slide=2
To Url: http://localhost/sholay-slide.jsp--slide=2