using slash in htaccess makes css files not to load - php

first, i'm trying to find an answer to this like 3 nights :) If there's answer of this, i couldn't find or understand.
My question is simple:
I want to split get parameters with slashes. And use this globally in my script.
in htaccess, i wrote this;
RewriteRule ^(.*)/(.*) index.php?param1=$1&param2=$2 [NC,QSA,L]
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
but this blocks (or sth like that) css files. when browser loads page, css file links looks normal but browser can't reach them (because of htaccess gets directories as parameters)
Thank you for your helps
edit: my full .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/(.*) index.php?param1=$1&param2=$2 [NC,QSA,L]
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]

Make sure both your rules look like this. Conditions only work for the first rewrite rule following them.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*) index.php?param1=$1&param2=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php?param1=$1 [NC,QSA,L]
Also use absolute paths for your CSS files or put a / before the file path.

By putting this above the rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It checks if the files already exist (eg the CSS files) and therefore won't try to rewrite or redirect them.

Related

How to create rewrite rule that allows to rewrite something right after slash with exceptions

i want to create these rewrite rules:
From:
www.something.com/name-of-some-product
To:
www.something.com/index.php?site=name-of-some-product
But I also need to use images and some other files and also other PHP files.
The .htaccess i have:
RewriteCond %{REQUEST_URI} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css)$
RewriteRule ^(.*)$ index.php?site=$1 [QSA]
The problem is when i try to load file like this:
www.something.com/another_php_file.php?parameter=value
It doesn't work, because i don't know how to avoid rewriting PHP files.
Try this Rule,
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?site=$1 [QSA]

htaccess Multiple URL Variable clean URL

Ok i've tried multiple versions of code on my .htaccess file and I still can't get it to work properly... I don't know if I'm putting it in the wrong folder or what is going on but, here is what I'm trying to accomplish.
This is my code
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([a-z0-9]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
Here is another version that I've tried
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
Here is what I'm trying to accomplish... a URL that looks like this:
example.com/rentals/32746/florida
The "32746" and the "florida" come from a form on the index page of the site and then it gets passed over to the "rentals" folder through the URL.
The .htaccess code sorta works whereas it spits out a URL like this:
http://www.example.com/rentals/12345/arkansas?zipcode=12345&location=arkansas
But do you see the extra on the tail end? It's as if it's duplicating the results in the URL.
I currently have the .htaccess file in my "rentals" folder should it be in the root folder?
UPDATED VERSION
For those who were running into the same problem as myself... here is the actual code I ended up going with:
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} /index\.php\?zipcode=([^\s&]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?zipcode=$1&location=$2 [L,QSA,NC]
You need to add a ? to the end of the target path in your first Rule :
RewriteEngine On
RewriteBase /rentals/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?zipcode=([a-z0-9]+)&location=([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ /index.php?zipcode=$1&location=$2 [QSA,L,NC]
As it descards the orignal query strings from the destination.
Clear your browser's cache before testing this.

mod_rewrite: dynamic redirect and directory blocking

I am working on an .htaccess-file lying in mysite.com/dir/.htaccess that redirects mysite.com/dir/page to mysite.com/dir/page.php if possible and redirect to mysite.com/dir/ if not possible. So any "wrong" request will be redirected to the main page. The code im using is:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [R]
This all works well. (By the way: i want to determine the base-dir of the file, because i maybe want to change the directory name later, but not change the file). What i now want is to block some directories for the user, because they contain php-libraries that should not be accessible. how can i e.g. block the directories mysite.com/dir/lib/* and mysite.com/dir/lib2/* and redirect them to mysite.com/dir/ again like i did before? I also want to use code not like
RewriteRule ^lib/(.)* %{ENV:DIR_BASE}
that would redirect mysite.com/dir/lib/../page to the main page instead of mysite.com/dir/page where it should belong. i tried very much with %{REQUEST_FILENAME} and %{REQUEST_URI}, but i am only a beginner when it comes to mod_rewrite. do you know a solution?
This should work:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
RewriteRule ^lib2?(/|$) %{ENV:DIR_BASE} [L,NC,R]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]

htaccess rewrite rule to remove parameters from URL

The user types the following into the browser and this remains the url in the browser:
xyz.com/info/productname/
Internally this becomes:
xyz.com/info/index.php?product=productname
The following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$ [NC]
RewriteRule ^info/([^/]+)/?$ /info/index.php?product=$1 [L,NC,QSA]
Is really close. Unfortunately the problem seems to be that the relative file links don't work when using xyz.com/info/productname/
See the difference between:
http://xyz.com/info/swoosh
and
http://xyz.com/info/swoosh/
The second one doesn't work because relative links are broken.
Take out leading slash from regex
Take out R=302
Add QSA
Make sure request is not for a file.directory
Modified rule:
# fix relative links of css, js, images
RewriteRule ^info/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [L,NC,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www\.)?xyz\.com$ [NC]
RewriteRule ^info/([^/.]+)/?$ /info/index.php?product=$1 [L,NC,QSA]

htaccess-rewriting, but excluding sub-directory [duplicate]

This question already has an answer here:
.htaccess exclude a directory to be rewrited
(1 answer)
Closed 9 years ago.
I have a general rewrite-rule in my sites root which looks like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
which works fine for rewriting URLs like "www.example.com/myAction" to "www.example.com/index.php?action=myAction"
This must stay like this, but I need to exclude a subdirectory ("/login") from rewriting.
So I tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/login/
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1
but still no luck. I've read several answers here on SO but can't find the flaw. Any ideas? Thank you!
You've an extra slash. .htaccess doesn't use the preceding slash unlike a Virtual Host. Also /login/ is a directory. So you're matching anything that is not a real file, but could potentially be a real directory. So if for instance '/css/' exists, you're rewriting it to index.php as an action on request.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
If it's still acting unexpectedly you might need to make sure you're not rewriting existing directories and try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+) index.php?action=$1
I also noticed that you're only capturing up to the first slash (possibly) with your posted example code since you're using the ? look-up, so in my previous layout's I'm showing that code a little shorter. To capture the whole URI you would use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^(.*) index.php?action=$1
To capture a second (and third) string you can do something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^login/
RewriteRule ^([^/]+)/([^/]+)/([^/]+) index.php?action=$1&page=$2&response=$3
What you have should work, but you can alternatively try to have all requests for /login/ be passed through instead:
RewriteEngine On
RewriteRule ^login/? - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w]+)/?([\w]+)? index.php?action=$1

Categories