Apache .htaccess acting strangely? - .htaccess

My .htaccess file looks like this:
#Options FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-zA-Z0-9\-\/]*[a-zA-Z0-9])$ index.php?_path=$1&%1
Here is my file structure:
example.com/
|- rewriteTest/
| |- images/
| | |- logo.png
| | |- icon.png
| |- style/
| | |- housekeeping.css
| | |- style.css
| |- .htaccess
| |- index.php
The desired behavior is that
example.com/rewriteTest/any/path
should point to
example.com/rewriteTest/index.php?_path=any/path
AND
example.com/rewriteTest/images
(without the leading slash) should point to
example.com/rewriteTest/index.php?_path=images
AND
example.com/rewriteTest/images/logo.png
should point to
example.com/rewriteTest/index.php?_path=images/logo.png
The rewriting works if the path is not an existing directory or file. However, if, say, I navigate to the /images directory, the url in my browser (Chrome) changes to /images/?_path=images and shows a 403 page. And if I navigate to /images/logo.png, the image opens up, instead of showing the page at /index.php?_path=images/logo.png.
Why is this happening and how do I fix this?

Your regex seems to be an issue. Try this:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /rewriteTest/
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{QUERY_STRING} !(^|&)_path=[^&]+ [NC]
RewriteRule ^(.+)$ index.php?_path=$1 [L,QSA]
Also to open a directory URL use a trailing slash like http://domain.com/images/

Related

How to redirect the Index of / to a website?

I made a VirtualHost to access to a php file and I want to allow access to this page and files in folder1.
Here are my folders :
root
|mywebsite.php
|.htaccess
|otherfiles
|
|--images
| |image.jgp
|
|--style
| |style.css
|
|--folder1
| |a_lot_of_folders
I can deny the access to the otherfiles in the root directory. If the URL is : www.ipaddress/ or www.ipaddress/otherfiles it will redirect to www.ipaddress/mywebsite.php.
But what I want is allow access only to the php file and files in folder1. But if the URL looks like this : www.ipaddress/folder1 or this : www.ipaddress/style it will open the Index of /folder and I don't want this.
Here is my .htaccess :
RewriteEngine on
RewriteRule ^$ mywebsite.php
RewriteRule ^otherfiles$ mywebsite.php # This is for each files in the root folder except the php one
RewriteRule ^/folder1? mywebsite.php # This doesn't work
I tried to rewrite the URL when it has /folder1 but it does nothing
Try this as your last rules:
RewriteRule ^folder1/?$ mywebsite.php [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^folder1/ mywebsite.php [L,NC]

htaccess redirect to subfolder and file

I think there are many htaccess redirect samples but I can't find a case similar to mine.
I have many sub folder my root
/suppliers
|- /suppliers/abc
|- /suppliers/xyz
/merchants
|- /merchants/abc
|- /merchants/xzy
etc.
I want to redirect them when I type
/m/yyy -> /merchants/yyy/index.php
/m/yyy/abc -> /m/merchants/yyy/abc.php
/m/yyy/abc/ -> /m/merchants/yyy/abc/index.php
Can anyone advise me on how I can do that?
You may use these rules in site root .htaccess:
RewriteEngine On
# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]

Error when using RewriteRule on the second .htaccess file

I am developing on my localhost and i have a structure like so:
|public_html
| .htaccess
| [pagefolder]
| ......index.php
| ......[adminfolder]
| .............htaccess
| .............admin.php
As you can see, i decide it not to go crazy on .htaccess files. Just 2 is enough. One on my root and one on my protected admin folder.
when the url is localhost i just do the following on my root .htaccess to display the initial page:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/?$ pagefolder/index.php [L]
When i want to access the admin panel (url: localhost/admin), i just do:
RewriteRule ^admin/?$ pagefolder/adminfolder/admin.php [L]
So far so good (i think). I run into problems when im using the .htaccess in the [adminfolder]
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^award/?$ admin.php?v=award [L,QSA]
When i go url: localhost/admin/award i get error:
The requested URL /admin/award was not found on this server.
However, the rule in the [adminfolder] looks ok to me. Im guessing something is missing, but not sure what?
None of your rules match /admin/award.
Your last rule redirects /pagefolder/adminfolder/award to /pagefolder/adminfolder/admin.php?v=award.
You can fix this by redirecting more admin URLs:
RewriteRule ^admin(/.*)? pagefolder/adminfolder/$0 [L]
Then change your adminfolder .htaccess:
RewriteRule ^admin/?$ admin.php [L,QSA]
RewriteRule ^admin/award/?$ admin.php?v=award [L,QSA]

htaccess - dynamic Baseurl generated from subsubdomain

unfortunately I don't know how to start.
I have the following file structure:
.htaccess
|- site1
|- .htpasswd
|- index.php
|- site2
|- .htpasswd
|- index.php
So I need a dynamically baseurl depending on the sub-subdomain.
When I enter http://site1.preview.domain.com I want the baseurl pointing to site1.preview.domain.com/site1. But in browser it should still be http://site1.preview.domain.com
Furthermore I want to protect each "site"-folder. So is it possible to dynamicly point to the htpasswd?
Thanks in advance for suggestions.
In the root .htaccess you can have this rule:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(site1|site2)\.preview\. [NC]
RewriteRule !^(site1|site2)/ %1%{REQUEST_URI} [L,NC]

.htaccess - redirect from subdirectory to root directory for non existing files

Current directory structure:
wwwpublic
|+ spanish(language)
|-- index.php
|-- contact.php
|
|- index.php
|- aboutus.php
|- products.php
|- contact.php
In the root of wwwpublic directory I have files in the English language. Since site have Spanish language too, I have created separate directory named 'spanish'. Name of pages are exactly the same in both directories.
Now, some pages in spanish directory don't exist and I need to redirect request to such pages to root folder and retain name of file requested.
Example:
Visitor go to Spanish version, open index.php there, then he clicks on aboutus.php page (dont exists) inside spanish directory and then .htaccess redirects him to the /root/aboutus.php
Try something like this in your wwwpublic directory, preferably before any routing rules you may have:
RewriteEngine On
# conditions to check that current request doesn't point to a valid resource
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# condition to check that request is for the /spanish/ directory
RewriteCond %{REQUEST_URI} ^/spanish/(.+)$
# conditions to check if the /spanish/ is removed whether it would point to a valid resource
RewriteCond %{DOCUMENT_ROOT}/%1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1 -d
# all conditions match, rewrite
RewriteRule ^/?spanish/(.+)$ /$1 [L]

Resources