.htaccess redirect home.html to root but allow query strings - .htaccess

I want to 301 redirect home.html to the root to avoid duplicate content. I can do this like this:
RewriteRule ^/home.html$ / [NC,R=301]
However there is a legacy affiliate program that use home.html like this:
home.html?a=companyname
Is there a way to allow the affiliate links, but still 301 home.html?

The query string will be appended automatically. From the docs:
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

Related

Redirect URL with query string to different file and maintain query string

I have old URLs that use the following:
mydomain.com/?page=article&cid=149&aid=1554
I had to change the name of the old index.php (which that URL above 'used'), for various reasons... and the file name is now index_180311.php. So those old links no longer work. The query string that does not change is the 'page=' part... the rest can be variable.
So what mod rewrite command would I need to make old URLs with the 'page=' query string get redirected to the new file name/path at:
mydomain.com/index_180311.php?page=article&cid=149&aid=1554
so that they work again?
I've searched the boards and tried altering other htaccess commands from other threads, but have not been successful (it usually ends up crashing my whole site), as I think I created infinite loops.
Thanks much in advance.
I wouldn't do an external redirect, try something like
# check for the presence of the page parameter (and a value)
RewriteCond %{QUERY_STRING} ^(?:.*?&)??page=[^&]+
# silently pass requests with the parameter to the old one (the query string is passed on automatically when the substitution has no "?")
RewriteRule ^(?:index\.php)?$ index_180311.php [END]

post parameters to htaccess sef link

I have url for ex:
http://www.demo.com/a/abcd
And I use this htaccess to post this url's values
RewriteRule ^a/(.*)$ /details.php?sef=$1 [L,NC]
But I need to get new parameters posted the url
For ex:
http://www.demo.com/a/abcd?id=123&qu=11
So how can I get the id and qu variables values via this url without do any changes in URL?
From Apache's docs on mod-rewrite:
Modifying the Query String
By default, the query string is passed through unchanged. You can,
however, create URLs in the substitution string containing a query
string part. Simply use a question mark inside the substitution string
to indicate that the following text should be re-injected into the
query string. When you want to erase an existing query string, end the
substitution string with just a question mark. To combine new and old
query strings, use the [QSA] flag.
Since, you are writing the query string in a rewrite, use the QSA flag:
RewriteRule ^a/(.*)$ /details.php?sef=$1 [NC,QSA,L]

URL Rewrite keep %20 in Query String

I am trying to make a rewrite rule to move all pdf's on my site to point to a specific page and then use a Query String as the pdf's current file path to do a look up in a dictionary to see if that url is in my dictionary if it is redirect them to the correct page. The catch is my dictionary of urls has %20 and when I pull the query string it turns the %20 in a space. Thanks for any and all help.
Can you rewrite it to keep the %20 in the query string?
Example URL: /example/example/Big%20Small%20Something%20Pad.pdf
My Rewrite:
RewriteRule ([^/]*)\.pdf$ /redirectPDF.aspx?pdf=$1.pdf [NE,QSA]
Current Query String Output: Big Small Something Pad.pdf
What I want it to look like is Big%20Small%20Someting%20Pad.pdf
Try removing the NE flag. According to ISAPI docs:
http://www.helicontech.com/isapi_rewrite/doc/RewriteRule.htm:
noescape|NE
Don't escape output. By default ISAPI_Rewrite will encode all non-ANSI >characters as %xx hex codes in output.
So it looks like you simply want to omit the NE so that it'll encode the output like it does by default.
RewriteRule ([^/]*)\.pdf$ /redirectPDF.aspx?pdf=$1.pdf [QSA]

301 redirect URL with query string, removing part of the beginning URL, keeping the full query at the end

I'm trying to find a redirect that will remove part of a URL (in the middle), but leaves the query string in place at the end.
I can do this fine via a single url redirect, but there are hundreds of these urls so I'm trying to find a rule that might be able to do for all of them in one fell swoop, so i don't have to make one for each and any new ones will get redirected automatically.
I'm trying to remove 'search.php' from the urls, here is an example:
www.site.com/products/search.php?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
to redirect to:
www.site.com/products/?rPage=/items/listing/detail_handler.jsp?code=219592&units=Feet&item_id=2624472
Thanks for your time.
According to the documentation, the query string is passed through unchanged by default
RewriteRule products/search.php http://www.site.com/products/ [L,R=301]

Problems with sorting comments in a SEF url system

I use Commentics for my website and having problem with my SEF urls. I couldn't find a solution in the related forum.
I have a rewrite rule like this :
RewriteRule ^([a-zA-Z0-9_-]+)$ /kurum.php?sef=$1
so my urls like http://fxrehber.com/kurum.php?sef=xtb
turns into this:
http://fxrehber.com/xtb
When I try to sort comments, that does not work
My url look like this :
http://fxrehber.com/xtb?cmtx_sort=5&sef=xtb#cmtx_comments
Is there way to solve this with an extra rewrite rule, or am I in the wrong direction?
Thank you
At last I found the simple solution for that: I just added "[QSA]" at the end of the rule as below:
RewriteRule ^([a-zA-Z0-9_-]+)$ /kurum.php?sef=$1 [QSA]
Source : http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Modifying the Query String
By default, the query string is passed through unchanged. You can, however, create URLs in the substitution string containing a query string part. Simply use a question mark inside the substitution string to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine new and old query strings, use the [QSA] flag.

Resources