I am tring to accomplish simple task using mod-rewrite
I need url of such kind
http://localhost/docs/path/to/a/file/somefile_without_extension?d=file.txt
became downloadable so user will download a file with name file.txt as attachment
I use something like this trying to make it working but it not. Please note
somefile_without_extension
is normal image for example
RewriteEngine On
RewriteRule ^/ ?d={d}
Header set Content-disposition "attachment; filename=%{d}e" env=d
Try this:
RewriteCond %{QUERY_STRING} ^(.*)[\&|\?]*d=([\w|\d|\.|\%|]*)[\&]*(.*)$
RewriteRule .* - [E=FILENAME:%2]
Header set "Content-disposition" "attachment; filename=%{FILENAME}e" env=FILENAME
UnsetEnv FILENAME
Related
I'm looking for a re-write for an identicon generator where something like
images/1-2.png
Would be interpretted on the server as
images/index.php?one=1&two=2
But it would still show site.com/images/1-2.png in the address bar.
Assumed there's only one dash (-) in the file name
Doable?
This will do it, put this in your .htaccess file in the root of your application.
RewriteEngine on
RewriteBase /
RewriteRule ^images/(\d)-(\d)\.png images/index.php?one=$1&two=$2 [NC,L]
I'm trying to reformat my url to be a bit shorter. Right now the links end up as this: website.com/image?id=name.jpg
What I want to have the link come out as is m.website.com/name, without the file exension or image.php file in the url. I figure mod_rewrite is the way to do it, so any help will be greatly appreciated.
In order to make it so someone accessing the URL http://m.website.com/name gets served the content for http://website.com/image?id=name.jpg, you first need to check the hostname for m.website.com, then match the name part of the URI. Using that match, you can proxy the request (using a [P]) or, if both website.com and m.website.com are hosted on the same server, just simply internally rewrite. Try putting this in your .htaccess file in your document root:
RewriteEngine on
# check the host (NC = no case)
RewriteCond %{HTTP_HOST} ^m\.website\.com$ [NC]
# don't rewrite /image
RewriteCond %{REQUEST_URI} !^/image
# Match the first non-slash word and rewrite
RewriteRule ^([^/]+)$ /image?id=$1 [L]
This will rewrite http://m.website.com/name to /image?id=name.jpg, but it will not rewrite http://m.website.com/path/name. If you want paths (and everything else) to be included in the id parameter, change the ([^/]+) to (.*) in the RewriteRule.
I am having a bit of difficulty getting mod_rewrite to do what I need it to do.
We have a group of virtual subdomains in a Drupal install. So, academics.univ.edu, about.univ.edu, etc are all part of the same core Drupal install.
File access currently is academics.univ.edu/sites/all/academics/files/myfile.jpg. However this path will also work as about.univ.edu/sitse/all/about/files/myfile.jpg or any other valid subdomain.
We'd like to use mod_rewrite to accept academics.univ.edu/files/myfile.jpg and deliver the file from the above location.
Here's what I've tried:
RewriteCond %{REQUEST_URI} ^(about|academics|bursar|calendar)\.univ\.edu\/files\/(.*)$ [NC]
RewriteRule ^/sites/all/files/$1/$2 [L,NC]
I'm probably going about this the wrong way, but I wanted to check on it. I can get the subdomains to work by making separate rules using HTTP_HOST, but I wanted less rules in the file. Also, I can't get HTTP_HOST to work on sites that exist as a subdirectory in a subdomian. For instance, undergrad.univ.edu/biology/files/myfile.jpg should deliver /sites/all/biology/files/myfile.jpg
You can't match a host in the %{REQUEST_URI}, you need to use %{HTTP_HOST}, then use the %1 backrefernce to access that match. The actual URI can be matched in the rule itself. Something like this:
RewriteCond %{HTTP_HOST} ^(about|academics|bursar|calendar)\.univ\.edu$ [NC]
RewriteRule ^files/(.*)$ /sites/all/files/%1/%2 [L,NC]
The %1 references the match (about|academics|bursar|calendar) in the RewriteCond and the $1 references the match (.*) in the RewriteRule. So that example will take a request to http://about.univ.edu/files/foo.html and rewrite the request to /sites/all/files/about/foo.html.
Also, if this is in a virtualhost or server config, you need a "/" in between "^" and "files" in the RewriteRule.
I want http://server/path/app.json?a=foo&b=bar to map to http://server/path/foo.php?a=foo&b=bar using mod_rewrite. I have the following incantation in my .htaccess which doesn't give any joy
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Suggestions?
Update: Adding rewrite.log and error.log output (comments don't allow formatting)
I get the following in the rewrite.log
strip per-dir prefix: /Users/user/Sites/dir/app.json -> app.json
applying pattern '^([^.?]+).json(.*)$' to uri 'app.json'
rewrite 'app.json' -> 'app.php'
add per-dir prefix: app.php -> /Users/user/Sites/dir/app.php
internal redirect with /Users/user/Sites/dir/app.php [INTERNAL REDIRECT]
and the apache server log says
The requested URL /Users/user/Sites/dir/app.php was not found on this server.
If I read your question correctly you want:
http://server/path/app.json?a=foo&b=bar
Going to:
http://server/path/foo.php?a=foo&b=bar
Sowhen you capture (app).json $1 is app and $2 is your second parenthesis, it's ... nothing (the part between json and the ?). As everything after the question mark is the QUERY STRING and cannot be captured here. Your rewriteRule is working on the requested file, not on the QUERY STRING. So you didn't captured foo anywhere. For the QUERY_STRING you could use the [QSA] flag on the rewriteRule, that would simply append a=foo&b=bar after your rewrite.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php$2 [L]
Here you tell apache to reuse $1 (the filename without .json), so app.json will get redirected to app.php, not foo.php.
RewriteEngine On
RewriteRule ^([^.?]+).json(.*)$ $1.php [L,QSA]
Will redirect app.json?a=b&z=r to app.php?a=b&z=r.
Now if you really need to capture foo as the first QUERY_STRING parameter the rule will become harder. But you could do it like that (here instead of the first parameter I detect the parameter 'a=' and capture his value in %4):
RewriteCond %{QUERY_STRING} (.*)(^|&|%26|%20)a(=|%3D)([^&]+)(.*)$
RewriteRule ^([^.?]+).json$ %4.php? [L,QSA]
First of all - I have searched quite a bit for an answer in stackoverflow and via google but haven´t been successfull so far to find a possible solution any advice would be greatly appreciated.
problem is:
I have a page
www.mypage.com
and controll the language displayed via a GET parameter lang
-> www.mypage.com/index.php?target=1&lang=en
Now a client has registered a domain at united-domains (www.mypage.it) and wants me display the italian version of the page whenever the domain www.mypage.it is requested.
The provider united-domains offers a solution called URL-HIDING which basically seems to pass the the request to a URL+folder structure I provide (like www.mypage.com/lang/it - according to the specifications of united-domains it has to be a folder structure and may not be a file)
solution so far:
Calls to the domain: www.mypage.it will be maped to www.mypage.com/lang/it/ via the URL-HIDING option.
there an .htaccess file rewrites the REQUESTS to the actual target:
RewriteEngine on
## check if query string contains NOT 'lang='
## (lang might be changed by user after initial request)
RewriteCond %{QUERY_STRING} !lang=
## redirect to page
RewriteRule ^(.*)$ http://www.mypage.com/index.php?target=1&lang=it
## check if query string contains 'lang='
RewriteCond %{QUERY_STRING} lang=
## Keep the existing query string using the Query String Append flag
RewriteRule ^(.*)$ http://www.mypage.com/index.php? [QSA]
Problem is of course: the CSS, javascript, image,... files are included with an relative path
<link href="scripts/style.css" rel="stylesheet" type="text/css">
<script src="scripts/media.js" type="text/javascript">
<img src="images/logo.jpg">
PDF
therfore the relative request scripts/style.css is mapped to http://www.mypage.com/lang/it/scripts/style.css
I am sadly stuck the the URL-HIDING mechanism of united-domains and changing every relative path the /scripts/media.js is not an option as I´d have to change quite a bit of code.
Does anyone have a solution for this (my .htaccess knowledge is not the best I am afraid)
Couldn´t I just remove lang/it/ from EVERY request and additionally check if the QUERY_STRING contains the string lang= and if it does simple add ?lang=it
thanks to all that have taken the time to read so far - if anyone has a suggestion I´d be more than grateful !
stay well,
matthias
This seems to do the trick !
in folder:
www.mypage.com/lang/it/
I put:
RewriteEngine on
## redirect if just folder is requested
RewriteCond %{REQUEST_URI} ^/lang/it/$
RewriteRule ^(.*)$ http://www.mypage.com/index.php?target=1&lang=it
## otherwise redirects if file does not exist
RewriteCond %{DOCUMENT_ROOT}/lang/it/$1 !-f
RewriteRule ^(.*)$ http://www.mypage.com/$1 [QSA]