Redirecting to without .html - .htaccess

So I have already this in my .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
Now if someone enters:
example.com/test.html
how can I redirect him to:
example.com/test
i tried:
redirect /test.html /test
but it gave me Page is not redirecting properly...

You need another rule. Replace your current rule with this and see how it works for you.
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.html [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Related

.htaccess rewrite url with page name slash GET parameter

I'm trying to rewrite my URL from request/2 to request?number=2
I tried to write this in my .htaccess
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/(.*)$ request?number=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But the result is a 404 Error
Can someone help me?
With your shown samples, could you please try following. Please do clear your browser cache before testing your URLs.
RewriteEngine ON
##To remove .php extension eg--> someone hits http://localhost:80/test.php it will redirect to http://localhost:80/test
RewriteCond %{THE_REQUEST} \s([^.]*)\.php\s [NC]
RewriteRule ^ %1 [R=301]
#RewriteRule ^(.*)$ $1.php [L]
##All non-existing files/directories will come in this rule whose URI starts from request.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^request/([\w-]+)/?$ request.php?number=$1 [NC,L,QSA]
##All non-existing files/directories will come in this rule set apart from URI which starts from request which is taken care above.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Homepage + redirect value from GET parameter

I need open file.php as index on my website. On homepage will be opened file.php without redirect to www.website.com/file.php
Second rewrite rule is redirect www.website.com/?id={id} to www.website.com/{id}
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ file.php?id=$1
RewriteRule ^$ /file.php
RewriteCond %{QUERY_STRING} ^(.*&)?id=([^&]*)(&.*)?$
RewriteRule ^ %{REQUEST_URI}?%1%2%3
Help me with htaccess code?
You can have your rules like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?id=([^\s&]+)\s [NC]
RewriteRule ^ /%1? [R=301,L,NE]
RewriteRule ^$ /file.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ file.php?id=$1 [L,QSA]

301 dynamic redirects not correct working

I need to change old dynamic urls to new, so I have created .htaccess file, but something wrong and redirect to ERR_TOO_MANY_REDIRECTS problem.
Below my .htaccess file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.+)/$ /cat.php?name=$1 [QSA,L]
RewriteCond %{QUERY_STRING} ^name=(.*)$ [NC]
RewriteRule ^cat\.php$ /movie/%1/? [R=301,L]
You need to check the original URI, and redirect to the pretty URL from that:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /cat\.php\?name=([^\s&]+) [NC]
RewriteRule ^cat\.php$ /movie/%1/? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/(.+)/$ /cat.php?name=$1 [QSA,L]

.htaccess vanity urls and clean urls together

I've been able to set up .htaccess to enable vanity urls on my website. However, I'm encountering into a problem when I try to clean other urls on my website. I would give the scenario and my current .htaccess below:
Currently wwww.mywebsite.com/john redirects to profile.php?user=john. In profile.php I check to see user john exist in my database if false I redirect to a custom 404.php page. This work very well. The problem arises when I try to have wwww.mywebsite.com/photo/abcdefg be interpreted as www.mywebsite.com/photo.php?m=abcdefg. For some reason it believes photo is the name of the user and then rewrites it as if its a vanity url. Please see my .htaccess file code below:
ErrorDocument 404 http://www.mywebsite.com/404
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteRule ^([^\.]+)$ profile.php?username=$1 [L]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You need to do your redirect before any internal routing. Then, you need to explcitly match against /photo before you route to profile and change the .php -f:
ErrorDocument 404 http://www.mywebsite.com/404
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule ^ HTTP%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^photo/(.+)$ /photo.php?m=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^\.]+)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /profile.php?username=$1 [L]

site loading for invalid URL's and redirection error

currently I am using this htaccess access,but the site still loads for invalid url, it doesn't goto 404.
ex: http://www.couponcoder.in/babyoye.com -->works and main content
http://www.couponcoder.in/babyo ----> invalid url, it displays homepage
and site loads for both slash and without slash, I just want it to redirect non-slash
www.couponcoder.in/babyoye.com/
www.couponcoder.in/babyoye.com
can someone help out with this?
Options +Indexes
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z.]+)?couponcoder\.in$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? http://www.%1couponcoder.in%{REQUEST_URI} [R=301,L]
RewriteRule ^admin$ Admin/index.php?qstr=$1 [L]
RewriteRule ^(.*)/$ index.php?qstr=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]
I can't read if you want to have the non-slash version or if you want to redirect the non-slash version to the slashed one. I'm assuming you want the former.
...
#remove slash, and redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?qstr=$1 [L]

Resources