How to get Current htaccess directory - .htaccess

I have following strucuture
/var/www which is pointed by localhost
there is directory called mytestsite in it.
My rewrite rule
RewriteCond %{DOCUMENT_ROOT}/controllers/$1\.php -f
RewriteRule ^(.*)\.html$ index.php?lang=en&module=$1 [L]
And that would work only:
RewriteCond %{DOCUMENT_ROOT}/mytestsite/controllers/$1\.php -f
I tried with RewriteBase but this seems not to change %{DIRECTORY_ROOT}
Is there any way to dynamically point/get current htaccess relative/absolute path for check? Tried many variations.

Related

Rewrite image path and uppercase part of it

I have been battling with the following code for ages and for some reason cannot redirect the following:
/folder/htdocs/uk/images/my-image.jpg >> /folder/htdocs/images/UK/my-image.jpg
I have tried
RewriteRule ^.+/(\w){2,3}/images/(.*)$ http://%{HTTP_HOST}/folder/htdocs/images/$1/$2 [NC,L]
RewriteRule ^/folder/htdocs/([A-Za-a]{2,3})/images/(.*)$ http://%{HTTP_HOST}/folder/htdocs/images/$1/$2 [NC,L]
And a number of variations but I still have not solved it. Please can someone advise how I can do this?
I have tried redirecting it to a file - just to capture the pattern matches but again to no avail.
Have this RewriteMap defined in your Apache server config (vhost):
RewriteMap uc int:toupper
Have this rule inside htdocs/.htaccess directory (create it if it doesn't exist):
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)/([a-z]{2})/images/(.+)$
RewriteRule ^ %1/images/${uc:%2}/%3 [L,NE,R=301]

RewriteRule does not match if folder name set to "myfolder/"

Since we migrated to a new server, some of our pages are broken (404). Reason is we have 2 broken rewrite rules.
What's really strange is that they work if I change folder's name.
For example this work:
RewriteRule ^anything/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
This doesn't:
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/$ page.php?var=$1 [L]
I can't even find a trick to make 301 redirects, because my original "myfolder/" virtual folder never matches.
Any ideas what's going on? I was thinking it could be a rule override or something like that (as it's hosted on a multidom solution), but i don't have such rules in my main site at the root. It drives me crazy.
Thx!
In practice you probably want to do 2 things. Disable multiviews and also bypass rules if the request is a real directory.
Options -MultiViews #turn off automatic URI matching, can cause weirdness
RewriteEngine on
#stop here if the request is a real file or directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^myfolder/([a-zA-Z0-9-]+)/?$ /page.php?var=$1 [L]

htaccess 404 issue

I am working with a CMS, and up until today its been fine.
But i have discovered that the mod rewirte only works if the website is in the root directory. If I put the entire CMS into a folder, i get a 404.
Please help!
RewriteEngine on
Options +FollowSymLinks
RewriteRule ^([^/\.]+)/?$ /index.php?1=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?1=$1&2=$2&3=$3&4=$4&5=$5&6=$6&7=$7 [L]
Then you need to add the subfolder to your rewrite rule like this, for all of your rules.
RewriteRule ^([^/\.]+)/?$ /subfolder/index.php?1=$1 [L]
With your configuration apache will search for the index.php file in the root directory
Your script is extracting data that are between slashes like:
/test1/test2/ transfers into /index.php?1=test1&2=test2
but since you started using folder it works like:
/folder/test1/test2/ transfers into /index.php?1=folder&2=test1&3=test2
so the folder name is breaking your structure, you will need to fix every line with folder name or expression like this:
RewriteRule ^[^/\.]+/([^/\.]+)/?$ /folder/index.php?1=$1 [L]
So you will need to fix both regular expression and new path on every line, just replace folder with name of your folder and add [^/\.]+/ at start
If that doesnt work you may just need to fix regular expression only without adding /folder to the second part of line

htaccess rewrite showing redundant root folder

I've searched but haven't found a solution to this exact problem.
I have a website domain 'mainwebsite.com' and several other top-level-domains managed in the htaccess. The issue I'm having is with 'alternatedomain.com' and subdirectories.
#alternatedomains
RewriteCond %{HTTP_HOST} alternatedomain.com
RewriteCond %{REQUEST_URI} !/alternatedomain/
RewriteRule ^(.*)$ alternatedomain/$1 [L]
This works fine, alternatedomain.com happily serves up mainwebsite.com/alternatedomain .
The problem is when I have access any folder like
alternatedomain.com/folder
, the URL in the title bar changes to
alternatedomain.com/alternatedomain/folder/
and it's driving me crazy. Can't figure out what I'm doing wrong!
EDIT: It only happens when I try access alternate.com/folder, and not when there is a suffix /, so alternate.com/folder/ works fine, without the redundant folder name
Your help is most appreciated!
This is because mod_dir (that adds the /) does not know the url is rewritten.
You could just disable DirectorySlash with
DirectorySlash Off
This will however make requests to alternatedomain.com/folder result in a 404 not found. You can fix this with some rewriterule though. So the complete solution would be something like:
DirectorySlash Off
RewriteCond %{DOCUMENT_ROOT}/$0 -d
RewriteRule ^alternatedomain/(.+[^/])$ /$1/ [R,L]

How do I remove a trailing slash?

I just want to remove a trailing slash from a directory. For example I want /p/page/ to show up as /p/page. It just looks better, doesn't it?
However I've tried many different kinds of mod_rewrites but none have worked or something happened.
I just want this to apply to subfolders (even better, any slash in a folder in a folder in a folder like /a/b/c), not /p/ as this may affect other parts of my site in a negative way.
You can try adding the following line to your .htaccess file:
DirectorySlash Off
That solved the problem for me a while ago. Of course if the path is only / I don't think you can get rid of it.
Copy this code in your root .htaccess file (directly under DOCUMENT_ROOT):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L]
DirectorySlash Off
RewriteCond %{THE_REQUEST} ^GET\s(.*)/\s
RewriteRule ^ %1 [R=302,NE,L]
It will externally redirect http://localhost/blog/ to http://localhost/blog while still displaying default index.html or index.php or whatever under /blog directory.

Resources