I have got those two rules:
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteRule ^categories$ home.php?categories=1[L,NC,PT,R=301]
RewriteRule ^featured$ home.php?featurez=1 [L,NC,PT,R=301]
The problem is that teh categories work and the featured doesnt work.
works:
http://apps.com/iphone/categories
doesnt work:
http://apps.com/iphone/featured
The second rule that doesnt work sends me to here
http://apps.com/var/www/vhosts/apps.com/httpdocs/iphone/home.php?featurez=1
It seems to send me the root of the root of my directory and that whole thing is prefixed with the root of my site..why?
How is that possible.
Rewrite conditions only apply to the immediately following rule, so your condition doesn't apply to the "featured" rule at all. You'll have to duplicate it.
Apache tries to guess whether the target of a rule is a URL-path or a file-path, and it's guessing incorrectly. You can try to fix it by either including a rewrite base or make your targets absolute URL-paths.
I've already answered this in your previous question using either of those solutions will fix the file-path appearing in the redirect.
RewriteBase /iphone/
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteRule ^categories$ home.php?categories=1[L,NC,PT,R=301]
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteRule ^featured$ home.php?featurez=1 [L,NC,PT,R=301]
or
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteRule ^categories$ /iphone/home.php?categories=1[L,NC,PT,R=301]
RewriteCond %{HTTP_USER_AGENT} iPhone [NC]
RewriteRule ^featured$ /iphone/home.php?featurez=1 [L,NC,PT,R=301]
Related
at the end of my .htaccess i redirect all url that i consider faulty.
this works perfect but i need an exception for my own pc
for my firefox browser i want the server to react as every other request
for my chrome browsers i want full access to all files on the server
i do it this way and it works:
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !=/retpic.php
RewriteCond %{REQUEST_URI} !^/pcs/.*$
RewriteCond %{REMOTE_HOST} ^11\.11\.1\.11$
RewriteCond %{HTTP_USER_AGENT} Firefox
RewriteRule . index.php [L]
RewriteCond %{REQUEST_URI} !=/index.php
RewriteCond %{REQUEST_URI} !=/retpic.php
RewriteCond %{REQUEST_URI} !^/pcs/.*$
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$
RewriteRule . index.php [L]
but i end up doubling the whole code... is there a more elegant solution like combining the following with an AND statement? (i found something about an OR statement but not about AND)
RewriteCond %{REMOTE_HOST} ^11\.11\.1\.11$
RewriteCond %{HTTP_USER_AGENT} Firefox
edit: added more explanation on how the code works:
RewriteCond %{REMOTE_HOST} !^11\.11\.11\.11$
this is from the second part it excludes my ip from this rule so i can access all files on the server, this is important since i want to be able to access my cms
RewriteCond %{REMOTE_HOST} ^11\.11\.1\.11$
RewriteCond %{HTTP_USER_AGENT} Firefox
this parts includes my firefox browser when i am home so i can see if the website works with all restrictions in place. why do i have this rule: i was working on my site and restructuring some parts and it kept on working for me but when i was at a friends place i noticed it did not so i needed something to be able to check this at home.
You nee do apply little bit of boolean algebra to combine these rules into one.
Here is you can do it for you:
RewriteCond %{REMOTE_ADDR} !=11.11.2.11 [OR]
RewriteCond %{HTTP_USER_AGENT} Firefox
RewriteRule !(index\.php|retpic\.php|pcs/) index.php [L,NC]
We distribute different versions of a software product through a single download link. The delivery is based on the referer in conjunction with a default value, which works fine. In addition the user should be redirected to a 404-page, in case the wrong filename was used.
At the moment the .htaccess-file looks like this:
# stop directory listing
Options -Indexes
# turn rewrite engine on
RewriteEngine On
# force 404 if file name is missing or wrong
RewriteCond %{REQUEST_URI} !^(download_mac\.zip|download_pc\.zip)$
RewriteRule (.*) 404/index.html [L]
# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ domain_ab/$1 [L]
# last rule if no referer matches
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ default/$1 [L]
So I have one issue and one additional question with this file:
The first rule, to force 404, is very greedy and gets the error page every time, no matter what URL is called. I also tried single statements like RewriteCond %{REQUEST_URI} !^download_mac\.zip$ without any effect. How can I fix this?
How can I get rid of the filenames in any other rule? I tried things like RewriteRule ^(.*)$ default/$1 [L] but it gives me a hard time and an 500 Internal Server Error.
You can avoid repeating your filenames by using an Env variable like this:
RewriteRule ^(download_mac\.zip|download_pc\.zip)$ - [E=ALLOWED:$1,NC]
RewriteCond %{ENV:ALLOWED} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /404/index.html [L]
RewriteCond %{ENV:ALLOWED} !^$
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-a\.com [OR]
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-b\.com
RewriteRule ^ /domain_ab/%{ENV:ALLOWED} [L]
RewriteCond %{ENV:ALLOWED} !^$
RewriteRule ^ /default/%{ENV:ALLOWED} [L]
You can just move the rewrite rule to the end. The other rules handle the valid cases and if none of them matches the last rule applies
# an example based on the referer
RewriteCond %{HTTP_REFERER} ^http://([^.]+\.)*domain-[ab]\.com
RewriteRule ^download_(mac|pc)\.zip$ domain_ab/$0 [L]
# last rule if no referer matches
RewriteRule ^download_(mac|pc)\.zip$ default/$0 [L]
# force 404 if file name is missing or wrong
RewriteRule ^ 404/index.html [L]
Situation
I'm using Zend framework, and thus attempt to call the folder by address ends in a fiasco and we gets the error: 'Invalid controller specified'. I needed to hook up additional forum to application in a separate folder. I change .htaccess file as follow:
RewriteRule ^forum(.*)$ forum$1 [L]
and for a while it was good, until I realized the fact that the page exists in two versions in two domains (php recognizes domain [.pl/.co.uk] and selects the language). So I decided to separate forums as a two separate modules (for example, the folder for Polish version forum will be: forum_pl, and for UK it will be: forum_uk - two different forums)
What's the problem?
It needs to rewrite .htaccess file to work like this:
IF: www.domena.pl/forum
THEN: open to the script from the folder /forum_pl
IF: www.domain.co.uk/forum
THEN: open to the script from the folder /forum_uk.
The worst thing is that when I tried to fix .htaccess file adding RewriteCond then the first redirect no longer work correctly, even after returning to the original (shown above) version.
Please help me and sorry if I did not find answers already given earlier.
EDIT:
First working solution
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]
It's look like it wants to work but, problems that needs solution is (TODO):
When you type: www.domain.pl/forum it gets you to www.domain.pl/forum_pl/ but if you type: www.domain.pl/forum/ it gets you to www.domain.pl/forum/ (read from forum_pl). How to make that it works in first situation same, as in second.
EDIT:
My last solution:
#FORUM
#PL
RewriteCond %{REQUEST_URI} ^/forum$ [NC]
RewriteRule ^(.*)$ forum/ [R=301,L]
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteCond %{REQUEST_URI} ^/forum_pl(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domena.pl/forum/ [R=301,L] #Dosen't work properly (?)
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_pl/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_pl$1 [L]
#UK
RewriteCond %{REQUEST_URI} ^/forum_uk(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/forum/ [R=301,L] #Dosen't work properly (?)
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_uk/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_uk$1 [L]
Final, working version:
RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteRule ^forum_pl(.*)$ http://www.domena.pl/forum$1 [R=301,L] # Not working!
RewriteRule ^forum_uk(.*)$ http://www.domain.co.uk/forum$1 [R=301,L] # Not working!
# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]
# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]
It is a pity that you can call from your browser forum_uk and forum_pl folders manually.
Not sure if I oriented correctly in your long question with some evolution :-)
Lets try this:
# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]
# redirect to polish version of web under forum_pl if on .pl TLD and
# request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum/(.*)$ http://www.domena.pl/forum_pl/$1 [R=301,L]
# redirect to english version of web under forum_en in on .uk TLD and
# request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum/(.*)$ http://www.domain.co.uk/forum_uk/$1 [R=301,L]
If you need the rewites to act differently, let me know
I have this rewrite code:
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://stagingsite.com/site/mobile [R=301]
RewriteRule ^faq/$ /mobile/faq
The first line is working correctly. If the user is on an iphone then redirect to the mobile directory where the index page is displayed.
I also want users visiting:
http://stagingsite.com/site/faq
to get forwarded to http://stagingsite.com/site/mobile/faq if they're on an iphone but the last line of code above doesn't seem to be achieving this.
Any ideas what I have wrong?
RewriteCond directives only get applied to the *immediately following RewriteRule. So you have the condition that checks for iPhone, but that only gets applied to the redirect rule, and not the faq rule. You have to duplicate it:
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^(.*)$ http://stagingsite.com/site/mobile [R=301,L]
RewriteCond %{HTTP_USER_AGENT} ^.*iPhone.*$
RewriteRule ^faq/?$ /site/mobile/faq [L]
You should also include L flags so the 2 rules don't accidentally interfere with each other. And your regex and target needs to be updated to accept an optional trailing slash and the subdirectory.
taking out the slash before mobile as
RewriteRule ^faq/$ mobile/faq
works?
How do I prevent spriders crawling pages that start with mydomain.com/abc...
For example mydomain.com/abcSGGSHS or mydomain.com/abc6bNNha
I think I need to add some sort of regular expression to the web root's .htaccess, right?
With mod_rewrite enabled, you can do the following
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^YourBadSpiderName [OR]
RewriteCond %{HTTP_USER_AGENT} ^AotherBadSpider
RewriteCond %{REQUEST_URI} ^abc
RewriteRule ^$ http://mydomain.com/404.html [NC,L]
You'll have to update the spider names accordingly. If a bot changes his user agent, let's say to 'Mozilla/Firefox', you're out of luck..