Simple .htaccess rewrite rule doesn't work - .htaccess

I have the following .htaccess file. It refuses to access the following url structure:
www.example.com/test
Even though it accesses this fine:
www.example.com/test.php
I've ran some more complicated rewrite rules using this file and it worked just fine. For example:
RewriteRule ^tests/([0-9]+)/?$ /tests_page.php?id=$1 [PT,L,QSA]
I don't understand how this can be happening. What am I missing here?
#OPTIONS
Options -Indexes
Options +FollowSymLinks
#ACCESS TO THE .HTACCESS FILE
<Files .htaccess>
order allow,deny
deny from all
</Files>
#REWRITE ENGINE
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteEngine On
#PAGES REWRITE
RewriteRule ^test/?$ /test.php
Update:
I renamed the file to tests.php and changed the rule to this and it worked:
RewriteRule ^test/?$ /tests.php
This still does not explain why this is happening though.
Why can't I have the url folder name match the file name?

This appears to be a problem with MultiViews option. Disable it by using:
Options -MultiViews
line at the top of your .htaccess. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite module and makes Apache server match extensions of files. So /file can be in URI but it will serve /file.php.

Related

How to make an internal redirect using a file .htaccess?

I have the urls
https://example.net/app/ref/user/u123456
https://example.net/app/ref/user/u123456/
Need redirect both url to https://example.net/app/ref/user/index.php?referral=u123456
How to do it with a file .htaccess? Unfortunately, I couldn't do it, so I'm asking for your help.
UPD: Now me .htaccess is in the same directory as index.php:
<IfModule mod_rewrite.c>
RewriteEngine on
</IfModule>
AddType application/x-httpd-php .html
<Files 403.shtml>
order allow,deny
allow from all
</Files>
With your shown samples please try following htaccess rules file. Please make sure you keep your .htaccess rules file in your root folder; alongside with your app folder.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*/[^/]*/[^/]*)/([^/]*)/?$ $1/index.php?referral=$2 [QSA,L]

CSCart Htaccess and directory php file

I have a cscart installation, with the classic htaccess.
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(win) [NC]
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
But, there is a folder with a php script I need to be accessible. Anytime I try to access that script, it shows CScart 404 page. But if change the script in the subdirectory to any other different file extension, it suddenly works.
cscart.com/folder/file.php doesn't work
cscart.com/folder/file.html works
Would any of you guys know how to turn htacess for that folder to run and show cscart 404 page?
Please check if under folder cscart.com/folder/ is present any .htaccess, this may be the issue
Some cs-cart folder have .htaccess with content
Order deny,allow
Deny from all
<Files ~ "\.(js|css|png|jpg|gz)$">
order allow,deny
allow from all
</Files>
remove this line:
RewriteBase /
Or correct the path when moving the CS-Cart to another directory.
RewriteBase /some/other/path

Basis mod_rewrite rule results in 500 internal server error

I have this very basic rewrite rule, no matter what I try, results in an Error 500.
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
My httpd.conf file has the following content: (which seems OK to me)
<Directory "/var/www/html">
Options -Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_suphp.c>
suPHP_Engine On
suPHP_UserGroup webapps webapps
SetEnv PHP_INI_SCAN_DIR
</IfModule>
</Directory>
Any suggestions on what might be going wrong? I've also tried to add $ at the end of my rewrite rule.
The rewrite engine will loop repeatedly, until the URI stops changing, or the internal redirect limit is reached which causes the 500 error to be thrown. Your rule's target URI /folder/index.php will get thrown back into the rewrite engine and your same rule's regex matches it, ^folder/(.*). So you'll need to add some kind of condition to prevent the loop.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/folder/index\.php
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is simple, it simply won't apply the rule if it already starts with /folder/index\.php. You can also try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^folder/(.*) /folder/index.php?Alias=$1 [L]
This is a little less restrictive of a condition. It only applies the rule if the requested URI doesn't map to an existing file or directory. This assumes that when you try to go to /folder/blahblah there isn't a directory or file blahblah and that you want to route it through your index.php.

htaccess rewriterule ignore index.html

I'm using CodeIgniter and .htaccess to rewrite the URLs but I want it to ignore index.html.
This is so we can upload an index.html at any time as this will be a temporary landing page. With a link through to the main site on the index.php.
This is what is currently in .htaccess and I have set the server's directory index to index.html index.php
RewriteEngine on
RewriteCond $1 !^(index\.html|index\.php|js|img|fonts|data|css|uploaded|mobile_devices|audioplayer|emails|robots\.txt|archive_blog)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks for any help
that rather looks like you want to rewrite everything that does not really exist in your directory.
try this instead of your current RewriteCond:
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1
Instead of using RewriteCond to ignore the index.html file, you could instead restrict access to it directly through using the FilesMatch directive. FilesMatch accepts a regular expression which could filter based on file name (e.g., index.html) or any regular expression.
Blocking access to the index.html file
<FilesMatch "index\.html$">
Order allow,deny
</FilesMatch>
That would completely deny access to the index.html file. I will admit, that I do not know the negative effects this would have on search engine crawling.
To read more about the FilesMatch Directive see http://httpd.apache.org/docs/current/mod/core.html#filesmatch
As for the rest of the directories in that list you currently have, you could just lock down all directory access regardless of name. It would give you a little more coverage going forward.
Options -Indexes
To read more about the Options Directive see http://httpd.apache.org/docs/current/mod/core.html#options
In the end your new .htaccess file would look something like this:
# Protect specific files from access
<FilesMatch "index\.html$">
Order allow,deny
</FilesMatch>
# Hide directory listing for URL's mapping to a directory
Options -Indexes
# Follow all symbolic links in this directory
Options +FollowSymLinks
# General rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Why don't you use the built in environment function?
You could create a 'maintenance' environment and set the base controller to whatever you want. Then you would just need to edit the index.php file to specify the environment you want.

Need to use httpd.conf instead of .htaccess for rewrite

I am working on a host that blocks my .htaccess file from being used so I can not change my permalinks using it. I need to figure out what code to use and where to put it in my httpd.conf file to get the same effect.
The code in the .htaccess file is below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
You'll need to wrap that code in a <Directory> directive. Where it goes will depend on what else you've got in your http.conf file. See the Apache docs for more info.
However, as blockhead says; if your host won't let you use .htaccess files, you've got virtually no chance of being allowed near the httpd.conf file.
For example, if you'd like to block access to GoogleBot throughout your entire server (which may be comprised of hundreds of virtual hosts), you can add this to your httpd.conf file:
#setup the root dir
<Directory />
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} Googlebot
RewriteRule .* - [F,L]
</Directory>
This will send a HTTP 403 Forbidden to anyone who comes in with Googlebot in their user agent string. And this rewrite condition will be applied to ALL virtual hosts, by virtue of applying this to the "/" folder.

Resources