HTACCESS RewriteRule includes /var/www/html/ to URL - .htaccess

I have the next question to you about .htaccess files. This is my folder hierarchy:
ROOT
- docs
- index.html
- terms.html
- privacy.html
- .htaccess
This is following .htaccess file content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
When I do following request:
https://example.com/docs/terms
The link in my browser looks like:
https://example.com/var/www/html/docs/terms.html
And I want to achieve this:
https://example.com/docs/terms.html
When I do https://example.com/docs/ I receive content from index.php file. How to fix my issue?

How you see the redirect without the R flag?
Try to change
RewriteRule ^(.*)$ $1.html [NC,L]
by
RewriteRule ^(.*)$ /$1.html [L,R=301]
/ is the important thing

Related

Rewrite my URL to remove strings using htaccess

I need help with my htaccess file. I added the following to remove all .php extensions which is working fine.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
However I have a directory /users which I want to rewrite the URL strings. The current URL is like this:
aero.site/maki/users/index.php?t=mf5cc
I want the URL to look like
aero.site/maki/mf5cc
I used the following rewrite rule but it's giving me Object not found error:
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
My complete htaccess content is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index.php?t=$1 [L]
Kindly help me with the correct rewrite rule for the last line to turn aero.site/maki/users/index.php?t=mf5cc into aero.site/maki/mf5cc
FYI: my htaccess file is in aero.site/maki directory
I finally got it to work. Thanks to #misorude. Here's my updated .htaccess content:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^/.]+)$ users/index?t=$1 [L]

.htaccess to access the file but to ignore non existing directory in URL path for that file

I have my htaccess and it already does couple of good things (it hides www and .php in my url) but I also need to add one additional feature to it.
I have the main domain called: wdd.ie
I have a root folder called: wdd.ie
Most important files are kept in the root.
I have also the following file in the root: about.php
I love to access about.php file by calling non existing directory: www.wdd.ie/myweb/
And my full URL path looks like this: www.wdd.ie/myweb/about
I would like to access the file (stored in the root) to allow non existing directory in url but to ignore it
Could anyone help!? Please, please help!
As this new desired htaccess feature complexity is now probably beyond my skills.
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
</IfModule>
If I've understood correctly you want the following url www.wdd.ie/myweb/about to offer up content from www.wdd.ie/about.php but keep the url the same?
If so, then the following should work
RewriteRule ^myweb/about$ /about.php [L]

.htaccess is not working in my PHP MVC Framework

I am building my own PHP MVC framework from scratch on ubuntu. .htaccess file is not properly working in it. The folder structure and the .htaccess file content are as bellow.
Folder Structure
/MyMVCProject
/controllers
index.php
help.php
/libs
/modles
/views
.htaccess
index.php
.htaccess File content
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
When I type localh0st/MyMVCProject/help it gives me the following error.
Not Found
The requested URL /MyMVCProject/help was not found on this server.
Apache/2.2.22 (Ubuntu) Server at localh0st Port 80
And I'm sure I have enabled mod_rewrite.
Try to do it this way and from $_REQUEST array you can get url parameter and handle it in your index.php file.
Also for testing purpose add
echo <pre>;
print_r($_REQUEST);
echo </pre>;
at first lines of index.php
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule ^(.*)$ /$1 [R=301,L]
</ifModule>
Try following RewriteRule (Copied and bit modified from Zend framework)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ index.php?url=$1 [QSA,NC,L]
I encountered the same thing, in my case the error was produced when I had a help.php file in the same directory. Don't know exactly why this happens...
You need to add:
RewriteBase /MyMVCProject
to your .htaccess file.

htaccess mod rewrite - Fake Directories

I have a URL like this: http://www.domain.com/index.php?token=123
I want to rewrite the url to this: http://www.domain.com/123 or http://www.domain.com/123/
With this htaccess i can access http://www.domain.com/123/ but my css is then not loaded.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [L]
RewriteRule ^([^/]+)/$ index.php?token=$1 [L]
Regards
If I guess right than is your problem that you link relativ to your css file. So you need to correct the path to your css file.

.htaccess rule to overwrite friendly url behavior

I am using a CMS called Vivvo, and vivvo comes with a .htaccess file from default with the following rules:
RewriteEngine On
RewriteRule ^sitemap\.xml$ index.php [L]
RewriteRule ^(.*)(\.html|\.htm)$ index.php [L]
RewriteRule ^(.*)(\.rss|\.atom|\.txt)$ index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php [L]
There's a folder called book/ in my webroot that I need to be accessible from the outside. However whenever I type www.mydomain.com/book vivvo sends me to the customized 404 error page.
What rule can I use, in order to make the web server to ignore the "RewriteCond" and make users go to the right directory?
I tried this with no success:
RewriteRule book/ book/index.php [L]
Try this:
RewriteEngine on
RewriteCond %{REQUEST_URI} "/book/"
RewriteRule (.*) $1 [L]
# the rest of your rules...

Resources