htaccess redirection for download.php?fid=## - .htaccess

Is there a way to redirect this :
http://example.com/download.php?fid=##
to another url?
I tried to use Redirect 301 in htaccess but it is not working for me.
Basically, I have an script , where you click on a button and you download a file, that is stored in your website, in your root folder, but I want to store the file externally, for example in Dropbox. If you copy the hyperlink of the button, you get http://example.com/download.php?fid=## --- where ## is query string that identifies the file.
If I remove the ?fid=## part, it works, but then all the downloads will be redirected to the same file.
lets say i have
http://domain.com/download.php?fid=40
I've tried this
RewriteCond %{QUERY_STRING} id=40`
RewriteRule ^sub-dir/download\.php$ /https://dl.dropboxusercontent.com/s/c1x0u1m64i1i186/00431.doc?dl=0/? [L,R=301]`
and Ive tried
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/download.php$
RewriteCond %{QUERY_STRING} ^fid=40$
RewriteRule ^(.*)$ https://dl.dropboxusercontent.com/s/c7x0u1m14i1i186/00431.doc?dl=0 [R=301,L]

It seems the second block of code its working for me now

Related

.htaccess Redirect To Another Site's Homepage Only If URL Don't Exist

So this is what I'm trying to accomplish, and I'll do my best to explain...
Through the .htaccess file, I'm attempting to redirect all pages of one site to another site, but ONLY if the URL doesn't exist. If the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
So, going to www.johnny.com/boxing (no files) or even just www.johnny.com (no files) would redirect to www.johnpunches.com, since neither of those URLs work. And that would work for any URL on www.johnny.com that doesn't exist. It would simply redirect to the index page of www.johnpunches.com
However, going to www.johnny.com/members (where there is a working directory) would actually go to that URL and not redirect, because the page exists.
So, again, if the URL exists, then do nothing. Load that URL. If the URL doesn't exist, redirect to the homepage of another site.
RewriteEngine On
# File does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# Directory does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect to new domain if the above were missing
RewriteRule ^(.+)$ https://www.google.com/$1 [NC,QSA]
Change out google.com to whatever domain you are redirecting to.
Okay, I was able to get some assistance from another thread and put this together:
RewriteEngine on
RewriteCond %{http_host} ^johnny.com [nc]
RewriteRule ^(.*)$ http://www.johnny.com/$1 [r=301,nc,l]
RewriteRule !^folder($|/) http://www.johnkicks.com [L,R=301]
In the example above, the "folder" can be replaced with any directory that works. The second and third line force the www version of the site before the final rule has run so that any URL that DOES work, like johnny.com/folder would redirect to www.johnny.com/folder.
Two questions:
Did I do it correctly? Does the order of my rules work? Should I be doing it any differently?
Would I have to duplicate that fourth line for every working directory?

Redirecting “.htaccess” with same folder & file name

A while ago I rebuilt a site that was on a CMS. As expected the Google search console regularly comes up with URLs that generate 404 crawling errors which I would promptly redirect to their new equivalents using a 301 on a .htaccess file.
The issue is that I came across the URL of an old page which looked like this:
http://www.example.com/example
The new page is now called http://www.example.com/example.php.
There is, however, a folder called example too so if I add a redirect like this:
Redirect 301 /example http://www.example.com/example.php
everything in that folder also gets redirected, so a URL called http://www.example.com/example/product.php would now show up as http://www.example.com/example.php/product.php.
Is there a way to redirect the old URL successfully without having to rename the folder example?
Please clear your browser cache and then put the following code at main root .htaccess file :
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+example [NC]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [R=302,L]
The code above will stop directoryslash and make it off so no file without slash goes to be directory as default and then check if example exists in QUERY_STRING and the trick comes here in this line RewriteCond %{REQUEST_FILENAME}\.php -f,it will check if adding .php to this string , is that really file in directory or not ? and already we have example.php in root so , it will go to last line to redirect any string that passed the rules above to itself.php and that what you want exactly.
The above code will catch any example in only root directory with example only but if wanna inherit other sub-folders or apply this rule to entire site so , whenever there is string without extension the priority will be for the file , put the following code at your main root .htaccess file :
DirectorySlash Off
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [R=302,L]
After testing this code , if it is Ok , change 302 to 301 to be permanent redirection

.htaccess redirect in drupal that excludes node edit page

Currently I have the following htaccess redirect rule that detects any path that looks like
mysite.com/node/xxx and redirects it to a .php script that, in turn, finds the URL of the page and redirects the user to it (e.g. mysite.com/page/page.html)
RewriteCond %{QUERY_STRING} ^(page=(31|1))?$ [NC]
RewriteRule ^node/?$ /? [L,NC,R=301]
RewriteRule ^(../)?node/([0-9]+)$ noderedirect.php?nid=$2 [L]
What I want, however, is that also when user accesses
mysite.com/node
or
mysite.com/node?page=xxx
they get redirected to the main page
BUT
when they access
mysite.com/node/xxx/edit
the rule doesn't get activated.
I tried several options (stopping at the one above) and so far I have this, but it still enables users to access mysite.com/node?page=xxx (all the other conditions are working fine.
Can somebody help?
I just want it to go to that page, and not to do anything. but when it's /node/xxx or /node or /node?page=xxx i want it to redirect to noderedirect.php
If you want it to always go to the noderedirect, then try:
RewriteCond %{THE_REQUEST} \ /+node\?page=([0-9]+)
RewriteRule ^ /node/%1? [L,R=301]
RewriteRule ^node/?$ /noderedirect.php [L]
RewriteRUle ^node/([0-9]+)/? /noderedirect.php?nid=$1 [L]

Redirect /index.php from the end of the URL

Have a website with subpages all in this format:
mydomain.com/something
That's fine. But what is NOT fine is that you can also do mydomain.com/something/index.php (you can enter address in this format into your browser) and you still get the content on that mydomain.com/something.
I don't want those two possibilites to be available at the same time, Google doesn't like this. I want just one to be possible.
So what I want to do is whenever you type into your browser mydomain.com/something/index.php, you will be redirected to mydomain.com/something (without that /index.php at the end).
How should I write a .htaccess code to do something like this?
add the following lines to .htaccess in the root directory of your website
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^(.*?)/?index.php$ /$1 [R=301,L,NC,QSA]
Note: the first condition assure that no previous redirection is made (to prevent redirection loop)
Mordor:
You can try this in your .htaccess file:
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]

htaccess retwrite subfolder to the maindomain

my domain name is www.hangings.com.au where as the project is hosted inside the folder named hangings so I need to enter something as www.hangings.com.au/hangings
Can anyone tell me how can I rewrite htaccess file so that it will show the content hosted in www.hangings.com.au/hangings when someone enters as www.hangings.com.au in their browser.
I tried something like the below
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !www.hangings.com.au/$1 [NC]
RewriteRule ^(.*)$ www.hangings.com.au/hangings$ [L,R=301]
but I am getting a browser error "This Website has a Redirection Loop"
The redirect loop is because of www.hangings.com.au/hangings matches also the condition.
If it's enough for you, that calls to www.hangings.com.au/ are redirected to the subfolder, an nor deeper structure, the following should be enough.
RewriteEngine on
RewriteRule ^/$ /hangings [R]

Resources