I had a folder that contained 15000 images, i decided to put them in 10 folders.
The initial folder url where i had my 15000 images was :
www.mysite.com/images/games/
And the new folders' urls are :
www.mysite.com/images/games/1/
www.mysite.com/images/games/2/
www.mysite.com/images/games/3/
www.mysite.com/images/games/4/
www.mysite.com/images/games/5/
www.mysite.com/images/games/6/
www.mysite.com/images/games/7/
www.mysite.com/images/games/8/
www.mysite.com/images/games/9/
www.mysite.com/images/games/10/
How can I redirect my images to match their correct folder to get rid of 404 errors?
Given your expansion in the comments above:
I know which images go to which folder, but its a big list
I suggest putting that list in a text file, and using find and replace (or awk?) to transform that list into mod_redirect rules, e.g.
Redirect /images/games/oldname /images/games/8/oldname
Then paste those into a configuration file. If there's no actual algorithm that determined how these got sorted into directories, there's no program that can redirect requests, because it would require a formula to work from.
Much as I love to cite When Not To Use Rewrite, this is actually a case where mod_rewrite is called for.
You don't say how you distributed your images between the ten new directories, but I am assuming the images were numbered (e.g. 00001.png, 00002.png, ... 15000.png). Then you used the last digit to determine which image goes into which folder, i.e. 00001.png goes into the 1 directory, all the way up to 15000.png which goes into the 10 directory (I think - why didn't you name it 0?) That's what you did, right?
You don't mention what server you're using on this site, so I'm going to assume you're using Apache, right? In that case you'd edit the appropriate configuration file (I started trying to assume which one, but honestly it could be any one of four I can think of, depending on whether you have edit rights to the vhost configuration, or root on the server, or if you're on a Debian-flavored or Red-Hat-flavored distro, you are using Linux, right? You didn't say.) You'd make sure there's a RewriteEngine On in there somewhere, then you'd do something like this:
RewriteRule ^/images/games/(\d+)(\d)\.png$ /images/games/$2/$1$2.png
...except that you named the directory 10 instead of 0, so all the ones ending in 0 are going to get misdirected. Maybe we should try this?
RewriteRule ^/images/games/(\d+0)\.png$ /images/games/10/$1.png [L]
RewriteRule ^/images/games/(\d+)([1-9])\.png$ /images/games/$2/$1$2.png [L]
...and catch the 0s first, then everything else.
I'd check this answer, but you haven't given us anywhere near enough information to know how.
Related
I have a problem with the mod_rewrite module with Lighty.
I am trying to make this: example.com/index.php?search=whatever, appear as example.com/whatever or example.com/search/whatever (have not decided yet -- don't know much about SEO)
While I want it to act like above, I also want to exclude all physichal directories and all files, such as the directory /images/ and the files index.php, favicon.ico, style.css etc. from the rewrite, because it acts weird.
How would I achieve this? I've tried the following, which worked okay for what I wanted, but didn't really work with the exclusion of the directories and files:
url.rewrite-once = (
"^/([a-zA-Z0-9_-]+)" => "/index.php?search=$1",
"^/(images|js|wp-content)/(.*)" => "$0",
"(.*\.php|.*\.css|favicon.ico)" => "$0" )
By the way, what difference is there between this:
"^/([a-zA-Z0-9_-]+)" => "/index.php?search=$1",
And this:
"^/(.*)$" => "/index.php?search=$1"
To avoid having to add numerous RewriteCond directives checking that the visitor's request is not an actual file rather than an artificial path, I recommend going with the /search/whatever pattern in favour of the /whatever pattern. Then, so long as you never create an actual directory called "search", you'll never need to check whether a path beginning with /search is an actual file path. So your RewriteRule becomes this simple:
RewriteRule ^/search/([a-zA-Z0-9_-]+)$ /index.php?search=$1
(I'm not familiar with Lighty, so I'm not sure how to translate this into a url.rewrite-once instruction, but this is such a simple rewrite that it should be straightforward.)
However, the visitor's browser will now think that they are viewing a page which is in a sub-directory called "search", so if you have any image elements or CSS files specified with relative paths (paths not anchored to the root directory) such as src="images/photo.jpg" or href="stylesheets/clean.css" then the browser will think those paths are relative to the "search" directory and will ask your web server for /search/images/photo.jpg and /search/stylesheets/clean.css respectively.
There are two ways to do this. The first is to change all page decoration (images, stylesheets, JavaScript) paths to absolute paths. That is, change the path so that it begins with a forward-slash which represents the root directory of the website. So your image path would need to be changed to src="/images/photo.jpg" and your stylesheet path to href="/stylesheets/clean.css". The forward slash at the start tells the web browser that the path starts at the site's root directory, so there is no ambiguity.
The second option is to create convoluted RewriteRules to redirect requests for images, stylesheets, script files, etc, to the correct directories. This tends to become ugly and fragile if you have a lot of media types in a lot of different directories and you need them to work from a lot of different sub-directories (virtual and/or otherwise).
Which option you choose depends on your requirements and preferences.
Regarding your question about the difference between [a-zA-Z0-9_-]+ and .* the first pattern only allows letters a to z (lowercase or uppercase), digits, underscores and hyphens. The second pattern allows any characters. For security and debugging reasons, it's usually better to use the pattern which limits characters to only those which should be allowed. So I'd go with the first of those patterns, adding additional permitted characters if necessary, rather than allow all characters.
Here is my predicament.
I've inherited support on a website and tasked with moving it to a new host.
An issue I have encountered with this is that there is an uploads folder with over 93,000 files in it. I have to move these files into a 'Year\Month' directory structure, based on the date of the files, while keeping external links alive.
Putting aside the complexity of modifying the database information, the rows relating to the individual files, to reflect the new structure is it possible to create conditional rewrite statements.
What I mean is if a request is made to find a file in that directory, specifically in the root 'Uploads' folder, that there would be a list of corresponding ReWrite rules reflecting the new positions.
Would having this many rules have a serious performance issue?
I suppose I could simplify it further where instead of putting the existing files into 'Year/Month' structure I could put them into an alphanumeric structure based on the first character of the files i.e. files starting with symbols would all go in the 'Sorted\Symbols' folder, files starting with 1 will go in the 'Sorted\1' folder and so on.
Im going to assume you're using Apache
Yes it is possible to create conditional rewrite rules using RewriteCond
See here for more info: http://wiki.apache.org/httpd/RewriteCond
If you are sensible with how you set it up, it shouldnt have a noticable impact on performance.
For example if the first rewrite condition you can provide can eliminate all the files that wont be rewritten then it shouldn';t create a big performance hit.
E.g.
RewriteCond %{REQUEST_URI} ^/mydir
Only perform rewrites on requests with uri starting with mydir
Gang,
Long time sysadmin but first time poster to this excellent site, so, please be gentle.
I am not strong at REGEX yet and trying to do two things at once on our internally hosted "mediawiki" site.
We are running an otherwise pretty plain jane LAMP stack (centOS 5.x, Apache 2.x, PHP 5.x). We are root. We are using /etc/httpd/conf.d/wiki.conf and not using .htaccess. The physical path is /var/www/html/wiki/
I have partially successful results with some combination of the below, but I am not good enough to get it all the way there. I know that there are some mod_write studs on this site that I am hoping to avail.
I am following this recipe https://www.mediawiki.org/wiki/Manual:Short_URL/Apache so as to shorten URL's from www.example.com/wiki/index.php?=title=Garden_Store to www.example.com/wiki/Garden_Store
still allow the use of www.example.com/wiki/index.php?=title=Garden_Store should the user should choose to type out that syntax of URL. (I believe that is possible with mediawiki to use both style URL's at the same time. If it is impossible, then I will be forced to skip the short URL and use the style with the variable in it.)
Last, string special characters from the URL in the example like www.example.com/wiki/index.php?=title=Garden,_Store! ought to be this www.example.com/wiki/index.php?=title=Garden_Store .
Another example of that might be www.example.com/wiki/index.php?=title=Garden_Store,_Inc. ought to be www.example.com/wiki/index.php?=title=Garden_Store_Inc
One last example, us to make sure that I am communicating well, would be getting this "/title=Garden%20Store,%20Inc" but wanting this "/index.php?title=Garden%20Store%20Inc" as I know that the spaces are replaced with underscores inside of mediawiki.
Thanks so much for walking a newbie the last bit to the finish line on this one.
Cheers.
Jason
Something like the following rules should do what you need:
RewriteRule ^(.*)\ (.*)$ $1\_$2 [L]
RewriteRule ^(.*)[^a-zA-Z0-9\/\._](.*)$ $1$2 [L]
First rule does replace space with underscore and the other line strips all chars you don't want to stay in the resulting URL. Note, your will probably need to add some more, if you want.
Hi i hve been running forum for quite while and people started using it for spam etc and now even though i took it down i still got like 100 clicks day for threads that dont exist and the forum is not existing too.
I tired of seeing this crap in my piwik stats i want to move it with mod rewrite so every time someone access
site.com/samples/forum he goes soemwhere else like actual redirect to fbi.com etc. better without showing my url of couse or just to some non existing folder so it does not triger my stats.
You're probably looking for something along the lines of:
RewriteEngine on
RewriteRule /samples/forum http://othersite.com/path [L,R]
(of course can flex if you need patterns, or for different paths) There are tons of handy options that you can use, all documented in Apache's documentation.
I want to create a url like www.facebook.com/username just like Facebook does it. Can we use mod_rewrite to do it. Username is name of the user in a table. It is not a sub directory. Please advise.
Sure, mod_rewrite can do that. Here is a tutorial on it.
Yes you can do this but you might have a couple of initial hurdles to get it going correctly.
The first is that you will have to use a regular expression to match it. If you don't know regex then this can be confusing at first.
The second is that you will need to take into account that of you are going to rewrite the top path on the domain you will have to have some mechanism for only rewriting if the file doesn't exist.
I guess if mod_rewrite supports testing if the url points at a real file that will be easy. If not you might have to use a blacklist of words that it wont rewrite as you will need to have some reserved words.
This would include at the least the folder that contains your images, css, js, etc and the index.php your site runs off, plus any other php files you have kicking around.
I would like to be more help but I am a .net guy and I usually help out in asp.net url rewriting issues with libraries such as UrlRewriter.net which have different configurations than mod_rewrite.
To match the username I would use a regex like this:
^/(\w*)/?$
this would then put the bit in the brackets into a variable you can use in the rewrite like
/index.php?profileName={0}
The regex I provided means:
^ nothing before this
/ forward slash
(\w*) any number of letters or numbers
/? optional forward slash
$ nothing after this