extra trailing slash mod-rewrite - apache

I got a problem of the extra trailing slash that directs my website from
http://www.example.com/index to http://www.example.com//index
here's my .htaccess content
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
# Apply rule to all web pages eg html, php, htm
# RewriteRule !\.(swf|zip|tar|tar|pdf|doc|txt|js|ico|gif|jpg|png|css|php)$ index.php [NC,L]
RewriteRule captcha.jpg com/actions/image.php
RewriteRule !(\.) index.php [NC,L]
#deny access to the .ht* files
order allow,deny
deny from all
#deny access to any *.ini files
order allow,deny
deny from all
#deny access to any *.log files
order allow,deny
deny from all

Get rid of the
RewriteBase /
By default, a .htaccess file in a directory will have a base path of / , so by adding another forward slash your overall rewrite base will be //

Related

htaccess restrict access to index.php only

I was writing a .htaccess file for my PHP script.
This should only allow access to the index.php, cronjob.php and execute.php pages.
I wrote the .htaccess file as follows:
# Set default index file
DirectoryIndex index.php
# Disable indexing
Options -Indexes
# Set "403 Forbidden" as the default server behavior
Order Deny,Allow
Deny from all
# Allow requests to core PHP files
<FilesMatch "(index|execute|cronjob)\.php$">
Allow from all
</FilesMatch>
# If mod_rewrite module exists...
<IfModule mod_rewrite.c>
RewriteEngine On
# ...restrict access to PHP private directories
RewriteRule (^|/)logs(/|$) - [F]
RewriteRule (^|/)utils(/|$) - [F]
RewriteRule (^|/)modules(/|$) - [F]
</IfModule>
The main problem with this code is that https://example.com/ returns 403 Forbidden,
while https://example.com/index.php works.
You could put condition to check if a URI is NOT having either index.php OR cronjob.php or execute.php then forbid that page so else other pages will be forbid apart from these 3 php uris.
Please make sure you clear your browser cache before checking your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_URI} !(index|cronjob|execute)\.php [NC]
RewriteRule ^ - [F]
In the end I managed to get it to work, I don't know if it's the best .htaccess possible, but this is what I ended up with:
# Set default index file
DirectoryIndex index.php
# Disable indexing
Options -Indexes
# Set "403 Forbidden" as the default server behavior
Order Deny,Allow
Deny from all
# Allow requests to core PHP files
<FilesMatch "^((index|execute|cronjob)\.php)?$"> # Basically, it accepts the three PHP files and the empty string.
Allow from all
</FilesMatch>

.htaccess rewrite everything to index.php except scripts in a specific folder

I have two publicly accessible folders:
/public
/legacy
Now I want redirect all requests to /public/index.php except those that start with /legacy:
/users --> /public/index.php
/legacy/somescript.php --> /legacy/somescript.php
currently I only have the following .htaccess files:
/:
Order deny,allow
Deny from all
/public and /legacy:
Order allow,deny
Allow from all
You could try this mod_rewrite rule
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(legacy/.*)$
RewriteRule . /public/index.php [L]

htaccess - Get error the page isn't redirecting properly

I'm trying to redirect all requests except some file types to download.php with this .htaccess file.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(\.php|\.html|\.css|\.jpeg|\.jpg\.png|\.js)$
RewriteRule ^(.*)$ /download.php?url=$1 [L,QSA]
I have place this htaccess in root folder, i get error from both firefox and chrome "The page isn't redirecting properly". If i move the htaccess in some sub directory, works perfect. Please help...
try the next code.
<Files ~ "^\.(htaccess|htpasswd)$">
deny from all
</Files>
Options Indexes
DirectoryIndex download.php
order deny,allow
deny from *.php
deny from *.html
deny from *.css
deny from *.jpeg
deny from *.jpg
deny from *.png
deny from *.js

htaccess redirect 301 before other rules

My htaccess look like this:
# Prevent directory listings
Options -Indexes
redirect 301 /old.html http://blablabla.pl/new
redirect 301 /other-page.html http://blablabla.pl/new-page
redirect 301 /xxx.html http://blablabla.pl/zzz
# Prevent visitors from viewing files directly
<FilesMatch "\.(sdb|md|html|txt)$">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</FilesMatch>
# URL rewrites
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(inc/|themes/|tmp/).*\.(php|html)$ - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
I'm using CMS that use html files as templates (there are variables, php code etc.). In htaccess of this CMS there are rules to prevent visitors from viewing html files directly. I moved one page to this CMS and wanted to do 301 redirect from old pages but it's not working - i'm getting 403 forbidden error. Is there a way to execute 301 redirect before other rules?
Working solution:
(...)
# Prevent visitors from viewing files directly
<FilesMatch "\.(sdb|md|html|txt)$">
<IfModule mod_authz_core.c>
Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
Order deny,allow
Deny from all
</IfModule>
</FilesMatch>
<FilesMatch "(old\.html)|(other-page\.html)|(xxx\.html)">
Allow from all
Satisfy any
</FilesMatch>
(...)

Why is the RewriteBase not working?

Here is what I am trying to do:
domain is thinkingmonkey.me
domain has 127.0.0.1 as IP addr
mod_alias is installed.
I have a conf file called directories.conf. In which I have all the configuration pertaining to directories. directories.conf is included in httpd.conf
My directories.conf has
Alias /runs /xhprof/xhprof_html
<Directory /mysite/xhprof/xhprof_html>
Order allow,deny
Allow from all
AllowOverride All
</Directory>
In /mysite/xhprof/xhprof_html/.htaccess. I have the following:
RewriteEngine on
RewriteBase /runs
RewriteRule .* index.php
All I am trying to do is to direct any request under /mysite/xhprof/xhprof_html/ to index.php.
When I request for thinkingmonkey.me/runs with no trailing slashes I get 404 not found.
So, I infer that RewriteBase is not working.
What am I doing wrong?
Alias requires a tailing slash. Nothing to do with RewriteBase.
You could use a rewrite rule to add the slash when needed though. Something like:
RewriteRule ^run$ /run/ [R=301,L]
Put this rule in the htaccess in the root of your server (not in /xhprof/xhprof_html)