Do I have to place "RewriteEngine on" every time when I place it in htaccess? - .htaccess

I've a pretty large htaccess about 400 lines and 30 of theme are filled with "RewriteEngine on" do I really have to place it every time before a script or is one time in the first line of the htaccess enough?
Thanks, Sake

As long as it's in the same scope as the rest of your rules, you only need it once, and it doesn't even need to be at the very top.
By scoping, I mean:
<Files "*.php">
RewriteEngine On
(some rules)
</Files>
(some more rules)
isn't going to work, because if the request isn't for a PHP file, the RewriteEngine On isn't going to get applied, and the "some more rules" part won't do anything.

Related

Can a Perl script set the address in the browser?

I would love it if my Perl script can change the address in the browser. Not to try and hide things, or be funny. But for cleanliness sake.
www.example.com/billing
looks much better than:
www.example.com/cgi-bin/billing.pl?userid=0000&inv=1234
I realize browsers probably have high security measures to prevent phishers from being more tricky, but what gives me some hope that this is possible is that I placed the following line in my .htaccess file:
ErrorDocument 404 /index.html
and if I go to www.example.com/nonexistent the browser will go back to the main page, but the address bar still has the nonexistent address.
Thanks

How to remove number in any url address in specific folder .htaccess

I want to remove numbers on the end url in specified folder, using htaccess.
(Numbers and minus sign befor numbers). For all urls in this folder.
For example
http://www.example.com/music/new-track-released-52
or
http://www.example.com/music/helo-there-4
Need to look like
http://www.example.com/music/new-track-released
http://www.example.com/music/helo-there
For all links in folder music
(I'm already removed php extension with htaccess)
How to do that?
Probably something like this:
RewriteEngine on
RewriteRule ^/music/(.+)-[0-9]+$ /music/$1
Note that this is the version for the host configuration. For .htaccess style files this has to be slightly modified. Whenever possible you should prefer not to use .htaccess style files but the real host configuration instead. Those files are notoriously error prone, hard to debug and really slow the server down.

.htaccess doesn't work if comment row (# ...) exist

code below doesn't work my .htaccess file. I mean, after this code is applied, I can still index folders in html.
# BEGIN disable folder index
Options All -Indexes
# END disable folder index
however, code below works. I mean, after this code is applied, server gives 403 if I try to index a folder which I know that it exists.
Options All -Indexes
I'm on a shared hosting and have nothing to do with server config. .htaccess is created via notepad++ with encoding setting UTF-8 without BOM. .htaccess permission is set to 0644. there exist no other code in .htaccess.
what does this situation mean? what am I doing wrong?
Ok, looks like my original comment above pushed you into the right direction:
Most likely this is a problem with the line breaks. So that for the
interpreting part of the http server that "Options" line is not on a
separate line, thus also commented out. Check your line ending
characters by using a hexeditor. That s the only reliable tool to do
so.

Can anyone think of when a sub-request rewrite is useful?

I found that one of the main things that cause .htaccess rewrite rulesets to do seemingly bizarre things is when Apache decides to try to apply them inside a subrequest. This is to the extent that I now always use the [NS] flag on my rules or use a prefix rule
RewriteCond %{IS_SUBREQ}%{ENV:END} t|1 [NC]
RewriteRule ^ - [L]
(The %{ENV:END} bit just allows me to use E=END:1 to do the same as the V2.4 END flag.)
My Q is: can anyone give me of a good usecase where I wouldn't want to do this? (or alternatively where I would want to use the special -U or -F condition patterns).
I realise that there may be many that I haven't thought of, but the A tick goes to the first valid one.
I'd guess the typical situations where you'd want to apply rewrite rules to subrequests are more or less the same as the one where you'd use symlinks inside your document root.
For a plausible example, let's say you're using Server Side Includes, and have a bunch of files scattered around with suffixes like .html, .shtml and .htm, and perhaps some uppercase variants of these too. At some point, you decide to standardize on the .html suffix, and rename all your files accordingly. But you still have a bunch of legacy code and links that use the other suffixes, and rooting them all out will take a while.
In that case, you might want a rewrite rule like this:
RewriteRule ^(.*)\.s?html?$ $1.html [NC]
By applying this to subrequests too, you ensure that your Server Side Includes don't break because of the renaming.

.htaccess or other URL Case Sensitive

My server is Case Sensitive, and id like to turn it to inSensitive.
Example of what I mean is
lets say I upload Fruit.php
Well then going to this file wont work:
www.website.com/fruit.php
but this one will:
www.website.com/Fruit.php
Is there a way so Fruit.php and fruit.php will work? also with the directories. i.e:
/Script/script.php
/script/Script.php
You need to use the mod_speling (sic) apache module:
http://httpd.apache.org/docs/1.3/mod/mod_speling.html
In .htaccess
<IfModule mod_speling.c>
CheckCaseOnly On
CheckSpelling On
</IfModule>
The CheckSpelling operative makes Apache perform a more involved effort to find a match e.g. correcting common spelling mistakes
Case sensitivity depends on the file system, not Apache. There is a partial solution, however. mod_rewrite can coerce everything to lowercase (or uppercase) like so:
RewriteMap tolowercase int:tolower
RewriteRule ^(.*)$ ${tolowercase:$1}
Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap
Unfortunately, this only works if all your files are lowercase, while you specifies mixed case filenames (Fruit.php.) Are you comfortable renaming all the files in your project lowercase?
UNIX-servers are case-sensitive - they distinguish between upper-case and lowercase letters in file names and folder names. So if you move your website from a windows to a UNIX-server (when you change web host for instance), you risk getting a certain amount of "Page not found"-errors (404 errors), because directories and other websites linking to yours sometimes get the cases wrong (typically writing the first letter of folder names in upper-case etc.). This javascript-based custom 404-error page solves the problem by converting URL's into lowercase.
You can get the script from http://www.forbrugerportalen.dk/sider/404casescript.js
Happy coding !!!!!!!

Resources