Replacing the content of the stylesheet in htaccess - .htaccess

I try change contents of stylesheet by htaccess, i have to use using HTTP_USER_AGENT and detect that current url is homepage (without any other in url, domain only).
RewriteCond %{HTTP_USER_AGENT} "android|ipad|iphone|ipod" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^css/main.min.css$ css/homepage.min.css [L,R=301]
it's doesn't work. Is also problematic when i try change line RewriteCond %{REQUEST_URI} ^/$ with RewriteCond %{REQUEST_URI} ^/contact -> example.com/contact does not load the home page stylesheet.
Without problem when i try redirect page to outside eg https://google.pl
RewriteCond %{HTTP_USER_AGENT} "android|ipad|iphone|ipod" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ https://google.pl [R=301,L]
Any idea how fix this problem?

Related

ht-access how to redirect to a forum topic url?

I would like to know how to use ht-access to redirect my pages which look as follows .com/?page=forums&topic=topic-name to be changed to .com/forums/topic-name?
The rule below redirects all the page names for example .com/forums or .com/polls which i currently use to get my pages to .com/index.php?page=forums.
RewriteRule ^(.+)$ index.php?page=$1 [L]
i would like www.treyarder.com/home to redirect to www.treyarder.com/index.php?page=home as well as www.treyarder.com/forums/topic-name to redirect to www.treyarder.com/index.php?page=forums&topic=topic-name
my topic is at www.treyarder.com/index.php?page=forums&topic=topic-name
my ht-access file as shown below
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [L]
RewriteCond %{HTTP_HOST} \.com [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1&topic=%2 [L]
ErrorDocument 404 /index.php?pages=404
ErrorDocument 500 /index.php?pages=500
there seems to be something wrong with the below line
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
does anyone know what it may be? as if i can fix that line then this should hopefully solve my question.
You must remove both ErrorDocument lines from your .htaccess as you're handling all non-file, non-dir requests through your front controller pattern.
You may try these rules in your site root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&topic=$2 [L,QSA]
## COMMENT OUT THESE 2 LINES
# ErrorDocument 404 /index.php?pages=404
# ErrorDocument 500 /index.php?pages=500
css/js/images issue: It is due to your use of relative links for these resources. Try ANY of these 2 work-arounds:
Use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /
Add this just below <head> tag of your page's HTML: <base href="/" /> so that every relative URL is resolved from that base URL and not from the current page's URL.
okay this was the final outcome which we managed to mix and match until it worked.
RewriteEngine on
RewriteCond %{HTTP_HOST} \.com [NC]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1&topic=%2 [L]
RewriteCond %{REQUEST_URI} !\.\w{2,4}$
RewriteCond %{REQUEST_URI} ^/([^/]*)/?$ [NC]
RewriteRule ^(.*)$ index.php?page=%1 [L]

.htaccess redirect to subfolder, non-www to www and https

So I'm having this website which is stored inside public_html/www/. Some other domains are stored in -for example- public_html/test/. In public_html I have a .htaccess file with a LOT of redirects in it, which all apply to this website inside /www/ folder. Inside the /www/ folder I have another .htaccess (I don't really know why, my colleague did this and he left me with this without information). Since I have no idea how .htaccess exactly works, I have no idea what to do and in which file.
What I need to do is to redirect domain.nl to https://www.domain.nl (some folders should be excluded, like you can see in the code below - emailtemplates, paymentupdate, autocode, nieuwsbrieven, etc.) But domain.nl is stored inside public_html/www/ folder, so it also needs a redirect to the subfolder (which should not be visible in web browser).
At this moment, in the root .htaccess file is the following:
# Use HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.nl [NC]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/emailtemplates
RewriteCond %{REQUEST_URI} !^/paymentupdate_ideal
RewriteCond %{REQUEST_URI} !^/autocode
RewriteCond %{REQUEST_URI} !^/nieuwsbrieven
RewriteCond %{REQUEST_URI} !^/bevestig-aanmelding-nieuwsbrief
RewriteCond %{REQUEST_URI} !^/bevestig-afspraak
RewriteCond %{REQUEST_URI} !^/images/personal
RewriteCond %{REQUEST_URI} !^/uploadify
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Link to subfolder
# RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]
# Non-www to www
#activate rewrite engine
#RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.nl
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
Probably half of this code isn't even necessary, but I have absolutely no clue. The other .htaccess file, within the subfolder /www/, has the following text which I don't really understand (it contains more, but nothing relevant):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.nl/$1/ [L,R=301]
I'm sorry if I sound stupid. I've tried many things copied from internet, but unfortunately everything has its concequences, which results in other problems with our website (links that don't work anymore, such as). I think the reason it doesn't work all the time, is because it's stored in the subfolder and I don't know how to combine all those lines in one like it would work.
Can anybody help me explain how to fix this and what is going wrong at this moment? Because the website is now not redirecting to www. It's just redirecting to https which gives me a security error at this moment (because the SSL is stored on www.). SUPER thanks in advance!
My problem was wrong order of the checks. First, I redirected to https://, but when I did not use www., it did not redirect to https://www OR it redirected to https://www.www. I fixed my problem by re-ordering my rules, like below:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
# Permanent redirect to https://
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/excluded_folder1/
RewriteCond %{REQUEST_URI} !^/excluded_folder2/
RewriteCond %{REQUEST_URI} !^/excluded_folder3/
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Use subfolder for domain
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteCond %{REQUEST_URI} !^/www/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /www/$1
RewriteCond %{HTTP_HOST} ^(www.)?www.domain.nl$
RewriteRule ^(/)?$ www/index.php?page=home [L]

After .htaccess rewrite to redirect root URL to subdirectory, files get not loaded properly

What I did was this .htaccess rule:
RewriteCond %{HTTP_HOST} ^(www.)?example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /static/somefolder/$1 [L]
In the folder is a static html and some css. The html now tries to load the css from root, which is clearly not want I want.
Actually I wanted all requests to be redirected, except for one folder.
So I then used:
RewriteCond %{HTTP_HOST} ^(www.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/downloads/.*
RewriteRule ^(.*)$ /static/somefolder/$1 [L]
But this throws an 'Internal Server Error'.
What did I miss here?
You need to exclude the destination you are rewriting to :
RewriteCond %{HTTP_HOST} ^(www.)?example\.com [NC]
RewriteCond %{REQUEST_URI} !^/downloads/.*
RewriteCond %{REQUEST_URI} !^/static/
RewriteRule ^(.*)$ /static/somefolder/$1 [L]
Otherwise you will get an infinite loop error because /static/somefolder/* also matches the pattern ^(.*)$

Internal htaccess redirect vs direct navigation

I have an .htaccess redirect script for my mobile detection:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (iphone|android|nokia|BlackBerry) [NC]
RewriteCond $1 !^mobile/ [NC]
RewriteRule ^(.*)$ http://www.example.com/mobile/ [R=301,L]
When I get redirected to the mobile site, my links don't work that point to:
www.example.com/img0.jpg
If I navigate directly to the site by browsing to:
www.example.com/mobile/
the links work perfectly.
What am I missing in the .htaccess file that is affecting this linking?
Thanks.
EDIT:
I used your script and then added this into the mobile directory, not exactly sure why it works, but it does!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mobile\.example\.com$
RewriteCond %{REQUEST_URI} example.com/mobile
RewriteRule ^(.*)$ /mobile/$1 [L]
Try this:
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (iphone|android|nokia|BlackBerry) [NC]
RewriteCond $1 !^mobile/$1 [NC]
RewriteRule ^(.*)$ http://www.example.com/mobile/$1 [R=301,L]

.htaccess language redirect with exclusion

For a multi-langue site I have used the following code to automatically redirect people to the right page. This works great, but does not include stylesheets and other files.
The following code in .htaccess turns www.site.com to www.site.com/fr if a visitor is from France. But it also redirects stylesheet and other files.
RewriteCond %{HTTP:Accept-Language} ^(nl.*) [NC]
RewriteCond %{REQUEST_URI} !(^/nl/.*) [NC]
RewriteRule ^(.*)$ /nl/$1 [L,R=301]
RewriteCond %{HTTP:Accept-Language} ^(fr.*) [NC]
RewriteCond %{REQUEST_URI} !(^/fr/.*) [NC]
RewriteRule ^(.*)$ /fr/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !(^/.*) [NC]
RewriteRule ^(.*)$ /$1 [L,R=301]
www.site.com/css/style.css turns into www.site.com/fr/css/style.css which doesn’t exist
www.site.com/js/file.js turns into www.site.com/fr/js/file.js which doesn’t exist
Is there a way to create an auto redirect, but exclude whole subdirectories? For example, exclude /css/. and /js/.
You can use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
so that requests to files and directories that exist will not be rewritten

Resources