.htaccess modify with different name files - .htaccess

I have links:
mydomain.com/index.php
mydomain.com/index.php?cat=1
mydomain.com/index.php?product=1
mydomain.com/index.php?view=content
mydomain.com/pages.php?view=order
mydomain.com/pages.php?view=register
mydomain.com/price.php
When I enter this links in browser, must get content from that files:
index.php.html
index.php_cat=1.html
index.php_product=1.html
index.php_view=content.html
pages.php_view=order.html
pages.php_view=register.html
price.php.html
How can I modify .htacces file for all this links?

Try this one:
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
#RewriteRule ^(.*)$ $1.php
RewriteEngine on
# general
RewriteRule ^index.php.html$ index.php
RewriteRule ^pages.php_view=register.html$ pages.php?view=register

Try :
RewriteEngine on
RewriteRule ^index.php\.html$ /index.php [NC,L]
RewriteRule ^price\.php\.html$ /price.php [NC,L]
RewriteRule ^([^.]+)\.php_([^=]+)=([^.]+)\.html$ /$1.php?$2=$3 [NC,L]

Related

Remove the .html extension in the URL, when the folder has the same name as the .html file

I have a problem with removing .html extensions from the website address. Everything works fine until I create a folder with the name that some file already has in the given location. For example, the situation with such files:
index.html
example1.html
example1/example2.html
When I try to go from index.html to example1.html, the page does not read the extension and shows me the folder tree instead of the page. It just goes to the example1 folder instead of example1.html. Anyone can help me? I've tried most (if not all) solutions to this problem with Google and nothing works :( I leave the content of .htaccess below. Thanks in advance
SetEnv PHP_VER 5_3
SetEnv REGISTER_GLOBALS 0
RewriteEngine on
# REMOVE .HTML
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
Okay, I found the answer to this question, just add these lines of code to .htaccess.
DirectorySlash Off
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(php|html)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*) /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*) /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\/$
RewriteRule ^(.*) %{REQUEST_URI}/ [L,R=302]

Modify url with .htaccess file

I would like to delete extensions like .php, .html, etc from my url and this is what I use:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([^\.]+)$ $1.html [NC,L]
It works but I don't know if this is the best way to do this and I also don't understand how it works, could someone explain it?
The second thing I would like to realise is to make the URL more beautiful by doing something like this:
this is my URL: http://domain.com/portfolio/project.php?id=1
this is what I would like to see: http://domain.com/portfolio/project/1
Thanks in advance
You can use these rules in site root .htaccess:
RewriteEngine On
# rewrite /portfolio/project/25 to /portfolio/project.php?id=25
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(portfolio/[\w-]+)/([\w-]+)/?$ $1.php?id=$2 [L,QSA,NC]
# rewrite /portfolio/project to /portfolio/project.php
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# rewrite /portfolio/file to /portfolio/file.html
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Two .htaccess files doesn't work together

I have two htaccess files and one of them located in root directory and other one in gifts/ directory.
Root .htaccess contains:
RewriteEngine on
RewriteCond %{REQUEST_URI} /gifts/
RewriteRule gifts/([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)$ gifts/catalog.php?catalog=$1 [NC,L]
RewriteCond %{REQUEST_URI} /gifts/
RewriteRule gifts/([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)/([0-9]+)$ gifts/catalog.php?catalog=$1&page=$2 [NC,L]
Gifts/ .htaccess contains:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^gifts.example.com$ [NC]
RewriteRule ^([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)$ /catalog.php?catalog=$1 [NC,L]
RewriteCond %{HTTP_HOST} ^gifts.example.com$ [NC]
RewriteRule ^([A-Za-z0-9]+(?:-[A-Za-z0-9]+)*)/([0-9]+)$ /catalog.php?catalog=$1&page=$2 [NC,L]
When I go to the following URL:
http://gifts.example.com/jeans
It works, but the following URL doesn't work
http://example.com/gifts/jeans
Not Found error occurs
Try gifts/.htaccess as:
RewriteEngine on
RewriteBase /gifts/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ catalog.php?catalog=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([0-9]+)/?$ catalog.php?catalog=$1&page=$2 [QSA,L]

How can i show beauty url with htaccess?

I'm using MVC with PHP and, for example, I have this URL:
domain.com/{modue}/{action}
This will make it look like:
domain.com/user/join
I want it to show
domain.com/join
instead.
Can anyone help me?
I tried this with an .htaccess file, but I couldn't. This is my code:
RewriteEngine On
RewriteRule ^$ ./controller.php [L]
RewriteRule ^([a-zA-Z_0-9-]+)/$ /$1? [R=301,L]
RewriteRule ^([a-zA-Z_0-9-]+)$ /controller.php?modulo=$1&%{QUERY_STRING} [L]
RewriteRule ^([a-zA-Z_0-9-]+)/([a-zA-Z_0-9-]+)/$ /$1/$2? [R=301,L]
RewriteRule ^([a-zA-Z_0-9-]+)/([a-zA-Z_0-9-]+)$ ./controller.php?modulo=$1&action=$2&%{QUERY_STRING} [L]
Try this if you have just one controller -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1
RewriteRule ^(.*)$ user/$1 [L]
</IfModule>

Looking for advice on mod_rewrite and htaccess

I am not experienced with htaccess... But I am authoring a site on which I am using a simple redirect. I basically just copied the first htaccess that I found and it works fine, but I am curious if I am using it properly and/or if it is responsible for some problems I am having with the site.
This is my file..
RewriteEngine On
Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} =""
RewriteRule ^.*$ /src/index.php [NC,L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^.*$ - [NC,L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
The index.php (as you can tell) lives in /src), as does most pf the site.
One problem I am having is that accessing some of the includes results in pulling back the index.php!
Any advice, or directions to some tuts or articles on htaccess would be GREATLY appreciated.
Thanks
Have to say that the lot of the code in here looks redundant. Replace your code with this:
RewriteEngine On
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_URI} ^/(dir1|dir2|dir3) [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !^/src/.*$
RewriteRule ^(.*)$ /src/$1 [L]
RewriteRule ^src/.*$ /src/index.php [NC,L]
Also when you say that ccessing some of the includes results in pulling back the index.php! I hope you are just including them like:
include('path/to/somefile.php');
That won't go through .htaccess since this inclusion is transparent to Apache.
Try combining a few of the rules:
RewriteEngine On
# Exclude some directories from URI rewriting
RewriteRule ^(dir1|dir2|dir3) - [L]
# block access to the htaccess file
RewriteRule ^\.htaccess$ - [F,L]
# route "/" requests to index.php
RewriteRule ^$ /src/index.php [L]
# route requests that exists in /src/ to /src/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/src%{REQUEST_URI} -d
RewriteRule ^(.*)$ /src/$1 [L]
# route everything else to /src/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /src/index.php [L]

Resources