Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule to display replace file with another

load non-existent file and display contents of another

         

TheKiller

3:18 am on Nov 22, 2014 (gmt 0)

10+ Year Member



Hello, i tried all possible ways to make urls case insensitive, but they were all useless as i run on a shared host.

This is what i need to do.

When InFILE_TX.ext is requested(it doesnt exist)
Display the contents of another file i specify manually at the same path.
The link must remain the same.



I tried something like this but it wont work....


Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ http://redir.domain.com/$1 [L,R=301]
RewriteRule ^InFILE_TX.ext?(.*)$ http://redir.domain.com/INFILE_TX.ext$1 [L,R=301]


Thanks in advance. i really need to sort this out. :-\

TheKiller

3:37 am on Nov 22, 2014 (gmt 0)

10+ Year Member



I got it ! :D

RewriteEngine on
RewriteBase /
RewriteRule ^InFILE_TX\.ext$ INFILE_TX.ext


I dont suppose there is a better way to detect the case automatically..?

not2easy

4:25 am on Nov 22, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



What you are using is an external redirect and what you want is an internal rewrite. Because it is internal it is relative and you don't need the http domain stuff.

Check out #20 in the Forum Library here: [webmasterworld.com...]

not2easy

4:33 am on Nov 22, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



I see you figured it out while I was typing, great. I'm not sure about the case problem - if you need to make part of this case insensitive, is it the rule or the target?

TheKiller

4:56 am on Nov 22, 2014 (gmt 0)

10+ Year Member



Hello, the rewrite i used is internal.. this was my reference.


From Old to New (intern)

Description:

Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. Actually we want that users of the old URL even not recognize that the pages was renamed.
Solution:

We rewrite the old URL to the new one internally via the following rule:

RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html



Is there no RewriteRule can have [a-z] and [A-Z] to make all urls case insensitive?

lucy24

6:54 am on Nov 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is there no RewriteRule can have [a-z] and [A-Z] to make all urls case insensitive?

Not if you only have use of htaccess. If it were your own server, you could install and use Apache's built-in RewriteMaps that convert to all-upper or -lower case so you can easily redirect any wrongly cased requests. (Technically you can use a RewriteMap in htaccess-- but only if it has previously been declared in config. Ordinarily a shared host wouldn't bother to do this.)

I'm not sure I understood the casing problem, though. Is your server case-insensitive, so
pagename.html
PAGENAME.HTML
PaGeNaMe.Html
et cetera all result in a valid page, leading to 2^n duplicates? Make sure it's a genuinely case-insensitive server, and not just mod_speling [sic] in its case-shifting mode. If it's mod_speling, you can and should disable it.

Next question: Is this all anticipatory, or are you already being vexed with a wide variety of wrongly cased requests? If it's just a couple of specific URLs, you can make page-specific rules like

RewriteRule ^Pagename\.html http://www.example.com/pagename.html [R=301,L]

or at worst, if a particular URL is a real mess,
RewriteCond %{REQUEST_URI} !pagename\.html
RewriteRule ^pagename\.html http://www.example.com/pagename.html [R=301,NC,L]

(note use of [NC] in the rule but not the condition)

RewriteEngine on
RewriteBase /~quux/
RewriteRule ^foo\.html$ bar.html

Please remove this source-- whatever it is-- from your Trusted Sources bookmarks. No need for a RewriteBase; it's better to express the rule simply as
RewriteRule ^foo\.html$ /~quux/bar.html [L]

As a general principle: Anything that can go in the body of a RewriteRule should go in the body of the rule (pattern and/or target).

Use the [L] flag with all RewriteRules unless you are sure you know what you are doing.


I guess the real problem is that I don't understand what the connection is between "InFILE_TX.ext" and case sensitivity. Is this one question or two?

TheKiller

7:49 am on Nov 22, 2014 (gmt 0)

10+ Year Member



Hello Lucy, thank you for your reply.

I have a directory (which serves as a game redirect) in which i have loads of game files.
The game server uses this directory to fast download files while in game.

The authors of the files didnt respect the case-sensitivity when naming their files.
For example, their main file requests for a file named EM_RunTime_T but the author named their file EM_Runtime_T.

The game cannot find EM_RunTime_T, and it cant download the existing file which is named EM_Runtime_T so it throws an error. This applies to a few files. I am guessing there is no other way around this, so i started checking for the wrong file case and directed the wrong case manually, to the correct file with the code bellow.


RewriteEngine on
RewriteBase /
RewriteRule ^InTrinDay_TX\.utx$ INTrinDay_TX.utx
RewriteRule ^Mossberganim\.ukx$ mossberganim.ukx
RewriteRule ^Landlord\.u$ landlord.u
RewriteRule ^EM_RunTime_S\.usx$ EM_Runtime_S.usx
RewriteRule ^EM_RunTime_T\.utx$ EM_Runtime_T.utx
RewriteRule ^machete\.utx$ Machete.utx
RewriteRule ^BDCrossbowSkins\.utx$ bdcrossbowskins.utx
RewriteRule ^AK-74V2Anim\.ukx$ AK-74V2anim.ukx
RewriteRule ^machetersounds\.uax$ macheteRsounds.uax
RewriteRule ^mossbergSounds\.uax$ MossbergSounds.uax
RewriteRule ^MP5HKanim\.ukx$ mp5hkanim.ukx
RewriteRule ^mp5hk\.u$ MP5HK.u



I guess this is the best way to do it anyway as it doesnt put as much use on the server to check the case of the entire directory. If you know something better, please let me know. Thanks :)

lucy24

8:34 am on Nov 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I don't suppose it's practical to hunt down your developer and flay him alive at this late date ;)

Looking purely at server load, a one-for-one redirect/rewrite is the most practical approach if it isn't possible to change either the original code or the filenames. But do put a / at the front of each RewriteRule target, and make sure each rule has the [L] flag.

Normally we recommend leaving a blank line after each RewriteRule for readability. But when you've got a whole bunch of closely related rules, go ahead and keep them all together.

TheKiller

9:39 am on Nov 22, 2014 (gmt 0)

10+ Year Member



Nah, the files were made by various developers. Their mods were good fun. :)

I've fixed the paths to your recommendation and ordered it nicely.
I'll leave it posted here in case i will ever lose the filenames :))

But i am curious about one thing, whats the role of the backslash, is it safe to remove it ?



RewriteEngine on
RewriteRule ^mp5hk\.u$ /MP5HK.u [L]
RewriteRule ^Landlord\.u$ /landlord.u [L]
RewriteRule ^machete\.utx$ /Machete.utx [L]
RewriteRule ^InUGSewers\.u$ /INUGSewers.u [L]
RewriteRule ^MP5HKanim\.ukx$ /mp5hkanim.ukx [L]
RewriteRule ^AK-74V2Anim\.ukx$ /AK-74V2anim.ukx [L]
RewriteRule ^InTrinDay_TX\.utx$ /INTrinDay_TX.utx [L]
RewriteRule ^Mossberganim\.ukx$ /mossberganim.ukx [L]
RewriteRule ^EM_RunTime_S\.usx$ /EM_Runtime_S.usx [L]
RewriteRule ^EM_RunTime_T\.utx$ /EM_Runtime_T.utx [L]
RewriteRule ^machetersounds\.uax$ /macheteRsounds.uax [L]
RewriteRule ^mossbergSounds\.uax$ /MossbergSounds.uax [L]
RewriteRule ^BDCrossbowSkins\.utx$ /bdcrossbowskins.utx [L]

lucy24

7:48 pm on Nov 22, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



whats the role of the backslash, is it safe to remove it

The target of a RewriteRule is either external (beginning with protocol-plus-domain i.e. http://www.example.com) or internal (everything else).

If a target begins in / (single slash) that's it: it's just like an internal link, where / means your domain root.

If a target begins in nothing (that is, it starts right in with directoryname/ or filename.html) then this is attached to the RewriteBase, which by default is / but can be set to something else. For security reasons it's better to include the / explicitly. There may be a post somewhere by g1smd that explains this in detail, but the short version is "Aw, heck, just do it".

As a general principle: the more stuff you can put as explicit plain text into the single line of your RewriteRule, the less work the server has to do.

TheKiller

11:23 pm on Nov 23, 2014 (gmt 0)

10+ Year Member



Thanks for the help lucy. :)

TheKiller

2:20 am on Nov 24, 2014 (gmt 0)

10+ Year Member



I realised that you understood me wrong.. My question was, why is the backslash ( \) neccesary before the extension dot.
For example mp5hk\.u

lucy24

2:47 am on Nov 24, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oops, my bad, you did specifically say "backslash" --and you're not one of those infuriating TV commercials that say "backslash" when they mean "slash"*.

A dot . in Regular Expressions has a special meaning: "any one character". That including a space if it's in a context where spaces might occur, for example in a User-Agent string. If you want it to mean a literal dot, you have to \. escape it.

Sometimes this does not make a huge difference, because there may not be anything other than a dot that can occur in the position you're looking at. At other times it can lead to Unintended Consequences** for example if you give an IP address as
1.2.
because the RegEx could legitimately interpret this as "102." or "1.23" or lots of other things.

In the specific case of ".html" the most likely error is if someone types in the URL but forgets the dot, like "pagenamehtml". Then the wrong thing might be captured.

As a matter of habit, it's best to escape all literal periods, rather than stop and think about whether it's absolutely necessary in this specific rule.

Note that you only, ever, need to escape things in the pattern (the stuff you're matching against, including Conditions if any). The target (the place you're redirecting to) stays in plain text. Escaping in the target is not likely to be actively harmful, but it's never necessary, and makes the code harder to read.


* A virgule, if you want to be snarky about it.
** Apache-speak for "the world as we know it will come to a crashing end".