I have a number of PDFs in a rails application which have been converted into single page applications and I would now like to redirect the URL from:
www.domain.com/public-pdf-path.pdf
to
www.domain.com/reports/new-non-pdf-url
I've tried redirecting in the routes.rb file like such:
get '/pdf-public-file-path.pdf', to: redirect('/reports/new-url-address')
but it does not hit this redirect. Thoughts on how I could redirect a user to a new URL from the public file path where the pdf lives?
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^DOMAINNAME\.pdf$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/newpath [R=301,L]
Hope it helps
I had this exact issue. For me, moving/renaming/deleting the PDF I was trying to redirect made it work. I used the same kind of call in routes.rb as the OP.
Related
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^?(.*) productfilter.php?category=$1 [NC,L]
This rewrite is returning :
category=productfilter.php
can someone point out what I am doing wrong here
In all likelihood, the issue is that the rewritten URL is getting re-rewritten. Rewrite directives, at least in .htaccess context, get re-executed after a URL rewrite. I think what you are trying to accomplish can be done with:
RewriteRule ^productfilter.php - [NC,L]
RewriteRule ^([^/]*)/?$ productfilter.php?category=$1 [NC,L]
The first RewriteRule just declines to rewrite when productfilter.php already starts the path.
I'm successfully rewriting a users profile page thusly:
RewriteRule ^([a-z0-9]+)$ /profile/profile.php?u=$1 [NC]
RewriteRule ^([a-z0-9]+)/$ /profile/profile.php?u=$1 [NC]
so site.com/username was site.com/profile.php?u=username
Easy.
But now.. I'd like to have standard pages n folders like this...
site.com/login
site.com/help
etc... but the site thinks these are usernames... I've added rules to differentiate them but they dont seem to pick up - ie the one below doesnt work..
RewriteRule ^/login/twitter/$ /login/twitter/index.php [NC]
It thinks login is a username.
Any ideas?
Thanks!
You should use conditional rewrites before your RewriteRule to skip over your reserved names and files and folders that already exist on server:
RewriteCond %{REQUEST_URI} !^/reserved1
RewriteCond %{REQUEST_URI} !^/reserved2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
REQUEST_URI is the resource requested in HTTP request line. The first two lines mean that the URI should not start with your reserved names and should skip over /reserved1 and /reserved2
REQUEST_FILENAME is the same as SCRIPT_FILENAME CGI variable and contains the full local filesystem path to the file or directory matching the request.
The last two lines mean to skip real files and directolries that already exist in server.
i know this question has been asked/answered several times already, but i just cant seem to get past it. so, my apologies...
while in the site dev phase, i want the browsers address bar to display: domain.com/dev/item
i would like the RewriteRule to change it to: domain.com/dev/index.php?p=item
im using the .htaccess located in the public_html directory for this. the .htaccess within public_html/dev, which works fine, is configured to redirect everybody who is not using my ip to domain.com. my apache version is 2.2.15 and i've confirmed that i am able to redirect using .htaccess, but the top level .htacess simply doesnt work. here is the code im using:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^dev/(.*)$ dev/index.php?p=$1 [NC]
#variations tried: ^dev/(.*)+$, ^dev/(.*)+?$
the, including variations, results in a 404, /dev/item not found. removing the dev/ from ^dev/(.*)$ results in the correct url format, but the following server variables:
[REDIRECT_QUERY_STRING] => p=
[REDIRECT_URL] => /404.shtml
[QUERY_STRING] => p=
[REQUEST_URI] => /dev/item
my goal is to make a small change to support this type of rewrite for both the live site and the dev copy. but i just cant understand what im doing wrong. i really would appreciate any help.
You need to specify the / at the beginning of the rewrite paths.
Try this instead:
RewriteEngine on
RewriteBase /
# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
# If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/dev/(.*)$ /dev/index.php?p=$1 [NC]
I would like to add some code to my .htaccess to redirect several different domains to a single domain. I have seen code that will do this for one domain such as:
RewriteCond %{HTTP_HOST} ^www\.example\.net
RewriteRule (.*) http://www.example.com/$1 [R=permanent,QSA,L]
But what is the best approach to have multiple domains redirected to a single domain?
Only redirect if the domain is not the one you want to redirect to:
RewriteCond %{HTTP_HOST} !(^www\.example\.com$)
RewriteRule (.*) http://www.example.com/$1 [R=permanent,QSA,L]
I wanted to achieve this
example.com/search_results/?action=search&username[equal]=PorscheSA
to
example.com/PorscheSA
I have used .htaccess for many websites to achieve this, but since this website is smarty based, it doesn't seem to work. Any help will be much appreciated.
Ok, this is the .htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
In order to achieve the following I tried this:
RewriteRule ^([a-zA-Z0-9-_+^/])/$ /search_results/?action=search&username[equal]=$1
and nothing happened.
Your question is still a little vague as to what exactly you expect to have happen, but since you've posted your .htaccess, I'll take a stab at it and see if I get what you were after.
The most obvious reason that your RewriteRule isn't working is that your test pattern doesn't match the URL you've provided as an example, as your RewriteRule requires a trailing slash (and only matches one character before that). Additionally, if you put it after the rules that you currently have in your .htaccess file, it will never match because the request will have already been rewritten to index.php.
I think that you want something like this...
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^.*$ http://example.com/$0 [L,R=301]
# Make sure we end and force the redirect so %{REQUEST_FILENAME} gets changed
RewriteRule ^[a-zA-Z0-9-_+^/]+$ search_results/?action=search&username[equal]=$0 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
However, if the search_results/ directory doesn't exist, then this is just going to be rewritten to index.php anyway, so I'm not sure what the purpose of the redirection would be in that case. And, if it does exist, since your test pattern matches pretty much anything I'd expect to see in a site path, then everything will be rewritten to the search_results/ directory, so very little (if anything) will end up at your site root's index.php.
Based on that, I feel like maybe there's some other criteria that you may have left out, but I could be wrong.