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.
Related
Alright, so the only way I know of changing links to not show the file extensions;
this yourwebsite.com/customlink.php to yourwebsite.com/customlink
is either creating a folder called customlink and shoving an index file in there. Or creating a 404 page which snoops around the URL and grabs whatever string is behind the last / and does whatever to show the proper content.
My question is if this way of solving the problem is straight out idiotic, or not? I'm doing this because in my mind it saves space, let me explain: Every page that needs a customlink are the same with some bits and content taken from other places, so instead of creating X amount of folders and index files that includes a main file, I'll just have one 404 file that can handle it.
I apologize in advance if this is really stupid
As you are not mentioning what server your pages are running on (Apache, IIS, ...?), I'll just assume Apache.
Put an .htaccess file into the root of your site with the following content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
It will internally rewrite everything without an extension to the corresponding php file, provided that:
the request is not a valid directory
a file with a php extension is in fact present
A more common approach is to route all HTTP requests to a single index.php using mod_rewrite in a .htaccess.
Then, based on the requested resource, the index.php outputs the appropriate file, whether it is generated from a database or nested in some other folder.
It's probably not a good idea to use a 404 page (an error page) for anything other than "File Not Found" instances.
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.
I'm new with URL Rewriting and dont know how to do this:
All sites after /ex1 should be redirect to index.php
My current script:
RewriteEngine On
RewriteRule ^(ex1|ex2)/?$ index.php [NC,L]
But this will only rewrite example.com/ex1/and not example.com/ex1/abcd
Thanks in advance ;)
Close to perfect, try this instead:
RewriteEngine On
RewriteRule ^(ex1|ex2)/? index.php [NC,L]
Note: the trailing $ in the regular expression has been removed. That one was the cause why only the exact urls example.com/ex1 and example.com/ex1/ were matched, but no longer ones. The trailing $ anchors the search to the line end, so the end of the url here. So if there is anything between the last matched characters (the ex1 or ex1/ in this case), then the expression will not match.
Another more general note: this pattern is only usable withing .htaccess style files, but not in the normal hosts configuration. There are situations when such files come in handy, for example if you do not have administrative control over the http server. But in general you should always prefer to place such things inside the http servers hosts configuration instead of using .htaccess style files. Reason is that such files are notoriously error prone, hard to debug and finally really slow the server down.
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 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