I have the following mod_rewrite using Proxy flag to redirect from one URL folder to another site subdomain as follow:
The .htaccess file placed inside http://www.domain.com/test/ folder:
RewriteEngine on
RewriteRule ^($|/.*) http://subsite.site.com/$1 [L,P]
The problem, images, CSS and links are not showing up properly. Links appear to be pointing back to: http:// www. domin .com/linkname.html
I've tried doing RewriteBase /test/ and / with no luck, and couldn't figure out any other way to do it.
What am I missing in above code to make it work with relative paths at destination URL?
Oh, you want to change internal content?
Mod_rewrite only changes headers, not content and you would definitely need something else like mod_proxy_html. However, rewriting content just to change urls can normally be completely avoided (assuming you have control of your content) by making all paths legitimately relative. In such cases all paths in content should be like: linkname.html or some_path_from_here/linkname.html instead of /linkname.html or some_path_from_here/linkname.html
Related
On my server I have an image cache where all the image files are saved with the hash of their original location as their file name, without extension. I want to serve the images via mod_rewrite, with their original filename at the end of the URL.
For example, the physical location of an image file on the server is
/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273
I want to serve this image with the url
https://example.com/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273/OriginalFilename.jpg
So, I added this to my .htaccess file:
RewriteEngine On
RewriteRule ^_img_cache\/([a-z0-9])\/([a-z0-9])\/([a-z0-9]+)\/.+$ _img_cache/$1/$2/$3 [L]
It should just strip off the original filename part and serve me the file.
I've tried this, but it gave me a 404 in return.
Unable to find my mistake, I made some slight modifications, added an extension to my test file, and changed the RewriteRule accordingly:
/_img_cache/a/3/a3a37b602a1df1addfc8f2ff5a9824e1c9bd5273.jpg
RewriteEngine On
RewriteRule ^_img_cache\/([a-z0-9])\/([a-z0-9])\/([a-z0-9]+)\/.+$ _img_cache/$1/$2/$3.jpg [L]
If I request the same URL, I get my image served without problems. But it's not really the way that I wanted. I want it to work with the files as-is; extensionless. And that seems to be the only variable causing it to fail.
Why is Apache claiming the file without an extension, after rewrite, does not exist, while I've verified multiple times that it does. Is it a bug or a feature? Or am I missing something else? :)
Try this adjustment:
RewriteRule ^(_img_cache/([[:xdigit:]])/([[:xdigit:]])/\2\3[[:xdigit:]]{38})/.+ $1 [END,DPI]
Refer to the flags documentation, which warns that L can cause looping, which is rarely what people want. It should usually be combined it with DPI, and END is preferable if you don't need to check the rewritten path against any other rules.
Here's the explanation for the simplified regex.
I am trying to rewrite a condition that can take any page that has numbers.html, lets say the like the following:
12345.html
33443.html
234545656.html
9797423.html
and just redirect it to index.php
RewriteRule ^([0-9]+)$ index.php [L]
I tried the following but it doesn't work, any help is very much appreciated.
Two immediate issues with your code:
you have to switch on the rewriting engine prior to using it and
if you want to match file names ending with .html, then you have to code that.
Have a try with this:
RewriteEngine on
RewriteRule ^([0-9]+)\.html$ index.php [L]
Then a few more hints:
the code you tried will only work in .htaccess style files. But the usage of such files has to be enabled first in your http server configuration.
a .htaccess style file has to be placed at the correct location, in this example within the folder actually holding the index.php file.
the rewriting module has to be installed and enabled, without those commands will not be available.
And a general remark:
Those .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server, often for nothing. You should always prefer to place such commands inside the real host configuration of your http server. .htaccess style files are only offered as a last option for situations where you have no access to that configuration, for example when using a really cheap shared hosting provider.
What I want is a Wordpress type of URL rewrite.
What I have now is:
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
This almost works rewriting everything except existing files (css,js etc files are not rewritten as expected).
The problems I have with this are:
.css, .js, Image and PHP files are also accessible by entering their file name. eg.: domain.com/style.css will be accessible by anyone.
I want some existing files(only php right now) to be redirected anyway. eg.: domain.com/movies.php should redirect to rewrite.php(include 404.php) and domain.com/movies or domain.com/movies/ would include movies.php page.
Ideally I would also be able to change user entered URL from domain.com/movies to domain.com/movies/ for consistency more than anything else.
I want to keep .htaccess to a bare minimum. Does wordpress rewrite everything including .css files?
What I want:
Some php files to redirect others not. eg: includes/func.inc.php
should not be accessible to the users, while movies.php should be
accessible but ONLY from this url domain.com/movies/
(not essential)
Change url to canonical url eg: domain.com/movies to
domain.com/movies/ (some resources on how to achieve this would be
nice). Note: domain.com/movies url should still work but appear with
a slash at the end either via rewrite or maybe by just adding a
slash with javasript (faster?)
Make .css/.js files inaccessible by the user. eg: domain.com/style.css should redirect the user to a 404 page
I need to redirect all requests for an image called my_image.gif, when it's requested from any subdirectory of /images/, to /other_dir/my_image.gif. So /images/foo/my_image.gif, /images/bar/my_image.gif etc would all go to /other_dir/my_image.gif but all other images in /images/ (and its subdirectories) would be unaffected.
I don't know if this makes a difference, but my_image.gif will never actually exist in /images/ or any of its subdirectories.
Is there an easy way to do this with .htaccess? I can only seem to find ways to redirect all missing images.
Using mod_rewrite you should add this to your .htaccess, for more info check out the manual
RewriteEngine On
RewriteRule ^/images/my_image\.gif$ /other_dir/my_image.gif
i'm trying to use iirf for what looks like asimple rewrite but it's not working.
what i need:
rewrite http://www.mydomain.com/ru to : http://www.mydomain.com?page=russian
the objective being that a get param would be sent but the user would see the first url on their browser's url bar.
i'm using the following code:
RewriteEngine ON
StatusUrl /iirfStatus
RewriteRule http://www.mydomain.com/ru http://www.mydomain.com?page=russian
does this go (the iirf file) on the site's root or in a 'ru' subfolder (tried both)?
what am i doing wrong or missing?
thanx and have a nice day :-)
The following should work:
RewriteRule ^/ru$ /?page=russian [I,L]
You should put this in the iirf.ini file in the web sites root folder.
Check http://www.mydomain.com/iirfStatus to see whether iirf was able to read your configuration file.
Also, you may use RewriteLogLevel with a value of 2 or 3 and RewriteLog to see whether the incoming url was rewritten, and how (or why not).