.htaccess acting up

espfutbol98

New Member
Messages
200
Reaction score
2
Points
0
I have had some url rewriting problems but I usually just worked around them. The overall problem is basically, a rewritten url that is similar to another is redirected. And example is terror.audio. I redirected all .audio files to a special folder that holds mp3s. For some reason, it likes to put the /error page in the place of just terror.audio. Here is my root .htaccess file:
Code:
Redirect /bank /~mason/bank
order allow,deny
deny from 66.249.*.*
deny from 220.162.207.62
deny from 38.119.107.*
deny from 207.44.237.18
deny from 82.192.79.7
deny from 74.125.75.1
deny from 218.64.91.94
allow from all
Options -Indexes
ErrorDocument 400 /error?400
ErrorDocument 401 /error?401
ErrorDocument 403 /error?403 
ErrorDocument 404 /error?404 
ErrorDocument 500 /error?500
SetEnv TZ Europe/Berlin
AddDefaultCharset UTF-8
DefaultLanguage hr-HR
DirectoryIndex index.php naslovnica.php index.html
RewriteEngine on
RewriteBase /
RewriteRule ^index.php$ naslovnica.php
RewriteRule ^login$ login.php
RewriteRule ^logout$ logout.php
RewriteRule ^naslovnica$ naslovnica.php
RewriteRule ^glazba$ glazba.php
RewriteRule ^guest$ guest.php
RewriteRule ^o_nama o_nama.php
RewriteRule ^kontakt kontakt.php
RewriteRule ^druge_stranice druge_stranice.php
RewriteRule ^projekti projekti.php
RewriteRule ^glazba glazba.php
ReWriteRule ^masta_ace masta_ace.php
RewriteRule ^mos_def mos_def.php
RewriteRule ^common common.php
RewriteRule ^jezik jezik.php
RewriteRule ^preuzimanja preuzimanja.php
RewriteRule ^druga_preuzimanja druga_preuzimanja.php
RewriteRule ^brisanje_račun delete_account.php
RewriteRule ^register register.php
RewriteRule ^izmjena_računu update_account.php
RewriteRule ^samo_korisnike samo_korisnike.php
RewriteRule ^moj_račun user_personal.php
RewriteRule ^moj_racun user_personal.php
RewriteRule ^kutak_za_korisnike kutak_za_korisnike.php
RewriteRule ^signup signup.php
RewriteRule ^linkovi linkovi.php
RewriteRule ^mc_sniper mc_sniper.php
RewriteRule ^other_music$ other_music.php
RewriteRule ^hrvatski hrvatski.php
RewriteRule ^korejski korejski.php
RewriteRule ^([^/]+).pdf /design/documents/$1.pdf 
RewriteRule ^([^/]+).zip /design/documents/$1.zip 
RewriteRule ^([^/]+).audio$ https://podaci.co.uk/design/audio/$1.mp3 
RewriteRule ^([^/]+).wma$ https://podaci.co.uk/design/audio/$1.wma 
RewriteRule ^([^/]+).mp3$ https://podaci.co.uk/design/audio/$1.mp3 
RewriteRule ^([^/]+).png /design/images/$1.png 
RewriteRule ^([^/]+).glazba$ https://podaci.co.uk/design/audio/mc_sniper/$1.mp3 
RewriteRule ^([^/]+).jpg /design/images/$1.jpg
RewriteRule ^ssl$ ssl.php
RewriteRule error error.php
RewriteRule ^hr$ lang.php?hr
RewriteRule ^en$ lang.php?en
RewriteRule ^~([^/]+)$ /users/$1/
RewriteRule ^~([^/]+)(.*)$ /users/$1$2
##################Login#################################
RewriteRule ^([^/\.]+)/logout$ logout.php?redirect=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/logout$ logout.php?redirect=$1&name=$2
RewriteRule ^([^/\.]+)/login$ login.php?redirect=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/login$ login.php?redirect=$1&name=$2
########################################################
How should I solve this? I have read, what I believe to be, all the documentation on the Apache website and have found nothing.

By the way, if you want to see how it fails, https://podaci.co.uk/terror.audio?403 shows the error?403 page when it should be (without the ?403 of coarse) an mp3.

Even the actual location (https://podaci.co.uk/design/audio/terror.mp3) with the ?403 gives the 403 error page. And without the ?403 the blank error page.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
"terror.audio" and "design/audio/terror.mp3" match "error", so the "RewriteRule error error.php" line takes effect. Rather than fixing just that one line, you can replace that line and all the lines that add ".php" extensions with the following:
Code:
RewriteCond %(DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.*)$    $1.php
RewriteRule ^/?error\.php - [L]
(Overall, you could make better use of regular expressions.) The first two lines append a ".php" if doing so names a file that exists. The third is an optimization to stop rewriting the error page URL.

An alternative is to turn on content negotiation (my personal preference) by adding "Options +MultiViews". With content negotiation, Apache will automatically add file extensions to URLs.

You should add the "last" flag ([L]) to every RewriteRule that doesn't need further processing. That way, you won't accidentally rewrite a URL in a later rule. This would also have prevented the problem with the "error" rewrite.

Another thing that might cause other problems is that "." matches any single character, so the ".audio" subpattern will match "/audio". If you want to match a literal period, escape the dot and use "\.". Note that the dot isn't special in character classes, so you don't need to escape it there ("[.]" will match a literal period and nothing else).

You probably don't need the "RewriteBase /". Why do you have it?

Applying all the above suggestions (and a few more fixes), you get:
Code:
RewriteEngine on

# force secure connection for audio files
RewriteCond %{HTTPS} !=on
RewriteRule ^/?([^/]+\.(audio|mp3|wma|glazba))$ https://podaci.co.uk/$1 [L]

RewriteRule ^/?([^/]+)\.(pdf|zip)$ /design/documents/$1.$2 [L]
RewriteRule ^/?([^/]+)\.(png|jpg|gif)$ /design/images/$1.$2 [L]
RewriteRule ^/?([^/]+)\.(audio|mp3)$ /design/audio/$1.mp3 [L]
RewriteRule ^/?([^/]+)\.wma$ /design/audio/$1.wma [L]
RewriteRule ^/?([^/]+)\.glazba$ /design/audio/mc_sniper/$1.mp3 [L]

RewriteRule ^/?index(\.php|$) naslovnica.php
RewriteRule ^/?moj_račun(\.php|$) user_personal.php

#alternative to the following two lines: Options +MultiViews
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^/?(.*)$    $1.php
RewriteRule ^/?error\.php - [L]

RewriteRule ^(hr|en)$ lang.php?$1
RewriteRule ^~([^/]+)$ /users/$1/
RewriteRule ^~([^/]+)(.*) /users/$1$2

##################Login#################################
RewriteRule ^([^/.]+)/log(in|out)$ log$2.php?redirect=$1 [L]
RewriteRule ^([^/.]+)/([^/\.]+)/log(out|in)$ log$3.php?redirect=$1&name=$2 [L]
########################################################
 
Last edited:

espfutbol98

New Member
Messages
200
Reaction score
2
Points
0
WOW! Thank you very much! :)

By the way, I'm not sure why I have the RewriteBase /. I guess I just found it with an online example of something else and didn't get rid of it for a lack of understanding what it is.

Some things I have found:
RewriteCond %(DOCUMENT_ROOT}/$1.php -f I think should be RewriteCond %{DOCUMENT_ROOT}/$1.php -f

With the audio files, I had to make a full url (ie https://podaci.co.uk) or else on the download page, it would be song.glazba.
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Some things I have found:
RewriteCond %(DOCUMENT_ROOT}/$1.php -f I think should be RewriteCond %{DOCUMENT_ROOT}/$1.php -f
Yeah, that's a typo. It's fixed now. In addition, I didn't give a rule that would rewrite "moj_račun" to "user_personal.php", which is also now fixed.

With the audio files, I had to make a full url (ie https://podaci.co.uk) or else on the download page, it would be song.glazba.
Ah, you mean you want the URL in the browser to reflect the true path. Do you want to allow non-secure access to the audio files? If so, you can just use a redirect flag. Replace the rewrite rules for audio files with:
Code:
RewriteRule ^/?([^/]+).(audio|mp3)$ /design/audio/$1.mp3 [R=301,L]
RewriteRule ^/?([^/]+).wma$ /rewrite/design/audio/$1.wma [R=301,L]
RewriteRule ^/?([^/]+).glazba$ /rewrite/design/audio/mc_sniper/$1.mp3 [R=301,L]
 

espfutbol98

New Member
Messages
200
Reaction score
2
Points
0
In addition, I didn't give a rule that would rewrite "moj_račun" to "user_personal.php", which is also now fixed.
I just decided to rename the actual php files to reflect the .htaccess rules. I think it is a better system and is easier when editing files.

Thanks again for all the help!
 

vlatkomk

New Member
Messages
11
Reaction score
0
Points
0
I want to restrict a page where someone comes from abroad to show another page and when the national network to the actual page opens.
I'm not familiar so much for help.
Thank you
 

ichwar

Community Advocate
Community Support
Messages
1,454
Reaction score
7
Points
0
I want to restrict a page where someone comes from abroad to show another page and when the national network to the actual page opens.
I'm not familiar so much for help.
Thank you
vlatkmok, please don't ask for help for your problem in someone else's thread. It's likely that you'll wait quite a while before someone finds your post. You're better off creating a new thread in http://forums.x10hosting.com/programming-help/ :)
 
Top