Trouble with .htaccess mod_rewrite - .htaccess

Please can somebody take a look at my .htaccess code to help identify what's wrong.
I am trying to amend all example.php, example.html and example.htm URL's to index.php?filename=example.html
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(index\.php) - [L]
RewriteRule ^([^/\.]+).(php|html|htm)$ index.php?filename=$1.html&%{QUERY_STRING} [L]
</IfModule>
When I enter a URL like example.html I'm taken to the default 404 and not index.php

Try:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?filename=$1 [L,QSA]
</IfModule>

I'm not using the apache .hatacces for a while,
But try this one:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !^.*/(css|img|js)($|/.*$) [NC]
RewriteRule ^(.*)$ /index.php?filename=$1&%{QUERY_STRING} [L]
Basically what you need is to be sure that the URL didn't have already index.php. You accomplish it with RewriteCond.
After you redirect all to index.php passing the filename as a parameter.
You can add more exception to the RewriteCond like on this example, all content with /css, /js, /img will not pass to this rule.
You can check this thread: Using .htaccess to reroute all requests through index.php EXCEPT a certain set of requests also

Related

Htaccess redirect to controller action don't work properly

I want to redirect to my contoller - SiteController.php and action actionLanguage. I think my htaccess is OK but can't reach the action. Everything that I am trying to put in the url leads me to admin/site/index and not to admin/site/language in my particular case. My .htaccess :
# prevent directory listings
Options -Indexes
# follow symbolic links
Options FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/admin/gii/?$
RewriteRule . backend/web/index.php?r=gii [L]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT]
RewriteCond %{REQUEST_URI} ^.*$
RewriteRule ^(.*)$ /frontend/web/$1
This line RewriteRule ^admin(/.+)?$ /backend/web/$1 [L,PT] must execute to backend/web/site/language when I type www.website.com/admin/site/languageright ? Or I am in some mistake? It redirects the request to the admin/site/index now. Appreciate every advice! Thank you in advance!
EDIT:
Backend .htaccess:
AddDefaultCharset utf-8
Options FollowSymLinks
IndexIgnore */*
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
RewriteRule ^static - [L]

How do I do simple flat link to .php file redirect?

My current .htaccess file is as
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php [L]
This is at the directory level, I do not want to have a site-wide .htaccess.
I can redirect ../file.html to ../file.php but I am not able to do it for ../file -> ../file.php
My URL would be www.domain.com/sub1/sub2/file/ The file that should be run is www.domain.com/sub1/sub2/file.php But I want this rule to work only under sub2. So the rule will be included in the .htaccess at sub2
What exactly I need to change on this code to be able to do that?
More specifically, why is this not working?
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^(.*)$ $1.php [L]
This is working as I want it to work:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ $1.php [L]
You can change your existing rule to this to work under /sub1/sub2/.htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /sub1/sub2/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^./]+)/?$ $1.php [L]

htaccess with specific folder exception

I've set up an htaccess file in order to create a small routing structure where urls redirect to index.php. I want to add an exception where if the user types in "/admin" and anything after, the url instead redirects to admin.php.
htaccess -
RewriteEngine On
RewriteBase /
RewriteRule ^admin/?(.*)$ admin.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Example: http://mysite.com/admin/settings/ should lead to admin.php?page=settings, while http://mysite.com/about/ should lead to index.php (not using any parameters here). Regexp is really not my cup of tea, especially when there are multiple conditions to consider. Ideas?
Edit: ^admin/([^/]*)/? solved it.
You may try this in one .htaccess file in root directory:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !admin\.php [NC]
RewriteRule ^admin/([^/]*)/? /admin.php?page=$1 [NC,L]
RewriteCond %{REQUEST_URI} !/admin/ [NC]
RewriteRule .* /index.php [NC,L]
For permanent redirection, replace [NC],L] with [R=301,NC,L]

htaccess, sub folder rewrite to index.php

how can I do this?
main link:
http://www.domain.com/?link=whatever/something/everythting
convert to:
http://www.domain.com/whatever/something/everythting
I tried with this:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?link=$1 [L]
But not working.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ /?link=$1 [L,QSA]
This should work for you:
RewriteEngine On
RewriteRule ^(.+)$ index.php?link=$1 [L]
Edit:
As #anubhava posted in his answer, you should check whether the requested file or directory exists or not in a RewriteCond.
This should work:
RewriteRule ^([^.]+)$ index.php?link=$1 [L]

I need a Wordpress folder to NOT be case sensitive without writing 200 redirects

I have a blog set up at blog.ftj.com/ACSM, it is hosted with Bluehost and their folder structures seem to be case sensitive. Is there something in the .htaccess file that I can adjust so that all possible combinations get redirected to the specific uppercase URL.
Another issue is that it seems that I need to redirect
blog.ftj.com/acsm/
with and without the forward slash.
Here is my current .htaccess file
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /ACSM/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /ACSM/index.php [L]
</IfModule>
# END WordPress
Please submit the full change if you would.
You need to place the following .htaccess in the root dir to rewrite all requests to /ACSM into /acsm
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/acsm$ [NC]
RewriteRule ^(.*)$ /ACSM [L]
RewriteCond %{REQUEST_URI} ^/acsm/(.*)$ [NC]
RewriteRule ^acsm/(.*)$ /ACSM/$1 [L]
</IfModule>
Sorry for delays, have not got an Apache at hands....

Resources