mod_rewrite redirect url if no file found to main page - apache

I want to be able to Access my scripts without the .php, .html etc., so I already wrote
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
##same for other extensions
in my .htaccess file (note: this file lies not in the root-path), but I also want to Redirect every incorrect request to my main page, so that www.mysite.com/dir/incorrect will be rewritten to www.mysite.com/dir/.
But my first try (RewriteRule ^ / [R] after RewriteCond) redirected me to www.mysite.com/, my experiments with RewriteBase (RewriteBase . and RewriteBase /) didnt work and I also noticed that many similar scriptredirect to www.mysite.com/dir/index.php (www.mysite.com/dir/index in my case), but I really want to Redirect to www.mysite.com/dir/. Is there any way to achieve this?

Have it this way:
RewriteEngine on
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php [L]
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]

Related

Mod Re-Write Struggling With Rule Not Working

So after a lot of testing - I've started to figure out my problems but still can't get the rewrite to work.
This is how my htaccess file looks now.
RewriteEngine On
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Rule 1 is the one I'm having problems with
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
My understanding is now if I type this url into my browser
https://abfielder.com/schemdetail/id/158
I should get
https://abfielder.com/schematicdetails?id=158
The test of this here
https://htaccess.madewithlove.com/
Tells me my rules are ok.
And rules 2 and 3 are working fine.
However when I try and access
https://abfielder.com/schemdetail/id/158
I essentially get the page not found error.
Ok think I've finally fixed this for anyone else who is having issues with mod rewrite where they wish to have a rule to remove the file extension and other rewrites working together the order matters. Lastly if you're on a shared host type thing you may also need to set the rewrite base. This is how my htaccess file looks now.
RewriteEngine On
RewriteBase /
RewriteRule ^schemdetail/id/(.*)$ schematicdetails?id=$1 [L,NC]
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://abfielder.com/$1 [R=301,L]
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
This allows me to have url's without the .php extension.
It also allows for
schemdetail/id/100 to be translated into schematicdetails?id=100

.htaccess - removing extensions

So, I've recently started rebuilding my website from scratch, and now I've gotten to the point where I should edit my .htaccess file to make the links more SEO-friendly, etc.
Currently, I am working on removing the .php, .html and other extensions from my webpages to get clean links. For example, I want: http://www.mega.co.rs/founder.php
to become
http://www.mega.co.rs/founder
Writing Options +MultiViews in my .htaccess file seems to solve the problem, when I navigate between the pages of my domain. However, when I open my browser, and go directly to the 'corrected' link, it gives me a the first link, with the extension. How can I solve this problem?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.(php|html) to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(?:html?|php)[\s?] [NC]
RewriteRule ^ /%1 [R=302,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# To internally forward /dir/file to /dir/file.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f [NC]
RewriteRule ^(.+?)/?$ $1.html [L]

mod_rewrite rules in .htaccess

I want to do change my document root using rewrite rules in my .htaccess file, and also redirect all requests to index.php except static resources
In other words:
if a file with the path prepended with www exists (i.e. www/{%request_uri}):
http://domain.com/css/main.css should be rewritten to http://domain.com/www/css/main.css
if that file does not exist, redirect to the app main entry point www/index.php:
e.g.
http://domain.com should be rewritten to http://domain.com/www/index.php
http://domain.com/hello should be rewritten to http://domain.com/www/index.php/hello
http://domain.com/hello?a=1 should be rewritten to http://domain.com/www/index.php/hello?a=1
I tried variations, however either it gives us 500 internal server error, or infinite loop. Here is one that gives 500 internal server error:
# try to find it in www/
RewriteCond {%DOCUMENT_ROOT}/www%{REQUEST_URI} -f
RewriteCond {%DOCUMENT_ROOT}/www%{REQUEST_URI} -d
RewriteRule ^(.+) www/$1 [L]
RewriteRule ^(.*) www/index.php/$1 [L]
This one also:
RewriteCond %{REQUEST_URI} !www/
RewriteRule (.*) /www/$1
RewriteCond {%REQUEST_FILENAME} !-f
RewriteCond {%REQUEST_FILENAME} !-d
RewriteRule www/(.*) /www/index.php/$1 [PT,L]
Can anyone help?
I think that's impossible with a single .htaccess but it's possible with two.
You need one in your root directory, /.htaccess:
RewriteEngine on
RewriteBase /
# We don't want infinite rewriting.
RewriteCond %{REQUEST_URI} !www/.*$
RewriteRule ^(.*)$ www/$1 [L]
And another in your www directory, /www/.htaccess:
RewriteEngine on
RewriteBase /www
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Well actually I made a stupid mistake! The % sign should be outside of the {} bracket!
This can be done in one .htaccess in root dir:
RewriteEngine on
RewriteCond %{REQUEST_URI} !www/
RewriteRule ^(.*)$ www/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^www/(.*)$ www/index.php/$1 [L]

htaccess issue causing internal server error too many redirects

so I am playing with .htaccess to have clean URLs in my codeigniter app.
in short, i am trying to:
1) remove index.php in urls (redirect permanent)
http://localhost/directory/index.php*
to
http://localhost/directory/*
http://my.domain.com/index.php*
to
http://my.domain.com/*
2) rewrite requests for certain controllers to index.php/[controller_name]
http://localhost/directory/controller1*
to
http://localhost/directory/index.php/controller1*
http://my.domain.com/controller2*
to
http://my.domain.com/index.php/controller2*
my htaccess file currently goes like this:
Options +FollowSymlinks
RewriteEngine On
#RewriteBase /
# Redirect index.php
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{THE_REQUEST} !/system/.*
RewriteRule ^(.*)index\.php((/)(.*))?$ /$1/$4 [R=301,L]
first issue:
this does not work for http://localhost/dir/index.php/controller1.
instead redirecting to http://localhost/dir/controller1, it redirects to http://localhost//controller1 ($1 return empty string?)
# Rewrite CI certain controllers
RewriteCond %{THE_REQUEST} directory/(home|other_controller) [NC]
RewriteRule ^(.*)(home|other_controller)(.*)$ /$1index.php/$2$3 [NC,L]
second issue:
this does not work for http://localhost/dir/home gives internal server error (too many redirects).
but if I test added R=301 code, it successfully redirect to http://localhost/dir/index.php/home. but this is not my intention to redirect, I only need to rewrite it.
please advise.. :)
Try with this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
You can place this in the directory the bootstrap file (index.php) is in.
If you have FastCGI implementation, you need to add a question mark in the rewrite rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Remove .html from URLs with a redirect

We have a website, unfortunately all the URLs have the .html suffix, its a Magento installation, Magento allows you to change this on the CMS, but again, unfortunately all this URLs with .html suffix have a good ranking in Google. We need to redirect to non .html.
So, consider the following scenario, we are rebuilding this site from scratch, so we have the same urls on the new site but without the .html suffix.
Now is: www.example.de/cool-shoes.html
Will be: www.example.de/cool-shoes
So www.example.de/cool-shoes.html will not exist anymore, and I've been trying a redirect with the .htaccess with no luck.
I've tried so far:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule (.*)index\.html$ /$1 [R=301,L]
and:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
but it doesn't seem to work...any ideas?
Ok so, after some research, and failing to achieve this with a rewrite rule, the following line of code worked:
redirectMatch 301 ^(.*)\.html $1
This is quite usefull to remove any url extension and avoid broken links, hopefully helps someone in the future...
cheers!
This will rewrite the url like so http://example.com/page.html -> http://example.com/page
# Remove .html from url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.
Options +FollowSymLinks -MultiViews
DirectorySlash Off
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Here's the solution that worked for me.
RewriteCond %{THE_REQUEST} \.html [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [L]
Follow the steps, and you'll be able to remove .html from url without modifying .htaccess file.
This should do the trick:
RewriteEngine On
RewriteRule ^(\w+)\.html$ /$1 [R=301,L]
Try this to putting in your .htaccess file
Redirect permanent www.mysite.de/cool-shoes.html
www.mysite.de/cool-shoes
this may be helpful to you
This is for URLs ending with .html /product/raspberrypi.html ---> /product/raspberrypi/ (/product/raspberrypi/index.php) the index.php is hidden. Took me awhile to figure this out. LOL...
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
You have to use 'REQUEST_URI' and add it before index redirect rules since it could be overridden by the application. Its important to know that its URI not a filename or directory we are trying to redirect, since the file names all have index.php in the root folders(Wordpress).