mod_rewrite remove query string on top of a rewrite rule - .htaccess

I have the following rewrite rule:
RewriteRule ^(.*)-task-(.*)\.html$ /index.php/task/name/$2\-task\-$1 [L]
When I tried to open:
/heru-task-number-1.html
It is working fine. HOwever, when there is a query string appended to it:
/heru-task-number-1.html?whatever=value
It is actually not calling the correct rewrite. Thus, I wonder how can I make sure so that both:
/heru-task-number-1.html
AND
/heru-task-number-1.html?whatever=value
are actually calling the same thing that is:
/index.php/task/name/$2\-task\-$1
I have tried to do this but to no avail.
RewriteRule ^(.*)-task-(.*)\.html\?(.*)$ /index.php/task/name/$2\-task\-$1 [L]
Thank you for your help or feedback on this.

This is fixed by inserting the following code at the top of htaccess:
RewriteCond %{QUERY_STRING} (^|&)fb_comment_id=
RewriteRule ^(.*)$ /$1? [L,R=301]
basically, what it does is that it will remove any extra query string that has fb_comment_id and redirect 301 to the one without query string.
Thank you #oddant and #Gerben for helping out!

Related

url rewrite for N number of querystring

I am in need of a URL rewrite to http://jzbeta.local/category.php?u=new-page&limit=all&page=2&style=new as below,
http://jzbeta.local/new-page&limit=all&page=2&style=new
So far my htaccess file has the following rule:
RewriteRule ^([a-zA-Z0-9\-]+)\/?$ /category.php?u=$1 [NC]
Where I can use only one query string.
Any help is greatly appreciated.
UPDATE
Once i changed existing rewrite as following, it worked as i expected.
RewriteRule ^([a-zA-Z0-9\-]+)\/?$ /category.php?u=$1 **[L,QSA]**
Thanks for the suggestions.
You can use the following rule :
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /category.php?u=$1&limit=$2&page=$3&style=$4 [NC,L]

.htaccess rewrite rule to change path to query string? [duplicate]

Ok, im pretty new at this and I would really appreciate some help, thanks!
How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/?url=contact
All i want to do is remove the query string
/contact
Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all
This was my solution:
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
To a user on your site, they will see and navigate to this:
http://example.com/contact
But the real page would be something like this:
http://example.com/index.php?url=contact
This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html %1
(or whatever if it's not index.html, index.php, whatever)
You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule
Before: https://example.com/index.php?user=robert
RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]
After: https://example.com/user/robert

htaccess redirect specific url to another domain

I want to redirect specific URL with params to another domain
http://domain.tld/buy/?w=X1234 to http://anotherdomain.tld/product.php?id=X1234
Here my htaccess
RewriteCond %{REQUEST_URI} !^/*buy/(.*)$ [NC]
RewriteRule ^/*buy/?w=([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
but not working. maybe the problem is param ?w=
Thank you
UPDATE: i solve the problem by using Query string, after reading How to REGEX and .htaccess rewrite url
RewriteCond %{QUERY_STRING} ^w=([a-zA-Z0-9]+)$
RewriteRule ^/*buy/$ http://anotherdomain.tld/product.php?id=%1 [NC,L,R=301]
Thank you stackoverflow and folks! i m start understanding regex from here ^_^
That's right, your params won't be picked up, but they can be passed on.
Something like;
RewriteRule ^buy/([a-zA-Z0-9]+)$ http://anotherdomain.tld/product.php?id=$1 [NC,L,R=301]
Where your original url would be; /buy/X1234

htaccess redirect with parameters not working

I'm struggling with an Apache rewriterule. I need to do the following:
Redirect permanently:
http://domain.com/folder/viewer/data/settings.xml?prevent_cache=4760
to
http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=4760
I've got the code below, it works without the url parameters but I can't seem to get it to work with parameters. Am i missing something?
RewriteCond %{QUERY_STRING} ^prevent_cache=([0-9]*)$
RewriteRule ^/folder/viewer/data/settings.xml$ http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R=301,L]
Cheers
Shaun
The only error I can see, is the leading slash / in the RewriteRule pattern. This should be
RewriteCond %{QUERY_STRING} ^prevent_cache=[0-9]*$
RewriteRule ^folder/viewer/data/settings.xml$ /siteid/includes/themes/siteid/swfs/viewer/data/settings.xml [R,L]
You don't need to append the query string to the substitution URL, because this is done autmoatically.
When everything works as you expect, you can change R to R=301. Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.
I can. Here are the rewrite condition and rule that you're looking for:
# once per htaccess file
RewriteEngine on
RewriteCond %{QUERY_STRING} prevent_cache=([0-9]*)
RewriteRule ^folder/viewer/data/settings.xml http://domain.com/siteid/includes/themes/siteid/swfs/viewer/data/settings.xml?prevent_cache=%1 [R=301,L]
But please considered this answer about the [R=301] flag: https://stackoverflow.com/a/15999177/2007055

IIS Mod-Rewrite QUERY_STRING/QSA problems

We use IIS Mod-Rewrite from MicoNovae for our IIS rewrites on Windows 2003.
We use the RewriteRule command, for example:
http://www.site.com/section35/page1/tiling-tools/
becomes:
http://www.site.com/search.asp?section=35&page=1&model=tiling-tools
I now have a situation where I need to append the querystring to the re-written URL, for example:
http://www.site.com/section35/page1/tiling-tools/?myid=dskajh34kjhsvkjh34
need to become
http://www.site.com/search.asp?section=35&page=1&model=tiling-tools&myid=dskajh34kjhsvkjh34
My rules are:
RewriteEngine On
RewriteRule /\.htaccess$ - [F]
RewriteRule ^/section([^/]+)/page([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/$ /search_results.asp?section=$1&page=$2&model=$3 [L]
I've tried the following, without success:
RewriteRule ^/section([^/]+)/page([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/$ /search_results.asp?section=$1&page=$2&model=$3&%{QUERY_STRING} [L]
RewriteRule ^/section([^/]+)/page([^/]+)/?([^/]*)/?([^/]*)/?([^/]*)/$ /search_results.asp?section=$1&page=$2&model=$3 [L,QSA]
Suggestions will be appreciated ('cos it's driving me mad!)
Thank you!
Apart from the query string appending, I'm not even sure your rewrite rule is working at all?!? I would suggest to use something like this:
RewriteRule ^/section([0-9]+)/page([0-9]+)/(.*)/ /search_results.asp?section=$1&page=$2&model=$3 [NC,L,QSA]
The NC flag makes your rewrite rule non-case sensitive and the QSA flag adds the query string you need.

Resources