.htaccess Adding trail slashes - php

I got this code from somewhere.. and what it basically does is remove the .php extension
RewriteEngine On
RewriteRule ^Start/?$ index.php [NC,L]
RewriteRule ^Info/?$ info.php [NC,L]
RewriteRule ^Gallery/?$ gallery.php [NC,L]
it does it work good, but because of some SEO stuff,, i need to add automatic traling slash at the end..
Now it is sitename.com/Start
But it has to rewrite itself to sitename.com/start/
Any solutions??

You should handle this with a 301 redirect, placing it above the rules that you already have defined:
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
It is also worthwhile to remove the optional trailing slash flag in the rules that you've already defined, so your resulting .htaccess file will look something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^Start/$ index.php [NC,L]
RewriteRule ^Info/$ info.php [NC,L]
RewriteRule ^Gallery/$ gallery.php [NC,L]
In order to avoid the first condition rewriting valid files too, you should add in another condition:
RewriteCond %{REQUEST_FILENAME} !-f
Finally, your .htaccess should look like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule ^Start/$ index.php [NC,L]
RewriteRule ^Info/$ info.php [NC,L]
RewriteRule ^Gallery/$ gallery.php [NC,L]

Related

PHP rewriting URL to remove .php extension

I am working on a project where I decided to get rid of the .php extension from the URL of my app. I am successful using the htaccess code given below. But the problem is, the page still loads using .php extension if typed manually or loaded from the history.
My current .htaccess code.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want it to always load with www.example.com/dashboard except www.example.com/dashboard.php even if dashboard.php is manually typed.
UPDATED .htaccess code
RewriteEngine On
RewriteBase /dateapp/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You can add redirect as first rule.
RewriteEngine On
RewriteRule ^([^\.]+).php$ /$1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You have to use END flag instead of L otherwise the redirects will end in infinite loop. This will work if the .htaccess is in your webroot folder.
In case you have .htaccess in 'folder' subfolder you will have to add RewriteBase
RewriteEngine On
RewriteBase /folder/
RewriteRule ^([^\.]+).php$ $1 [R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,END]
You may use this code in dateapp/.htaccess:
RewriteEngine On
RewriteBase /dateapp/
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

how to redirect rewrite rule link using .htaccess

How do I create rewrite rule to get pretty link using .htaccess. for example I have a link like this:
http://localhost/piratefiles/view.php?idp=2&cat=Animation&pst=In-this-Corner-of-the-World-(Kono-Sekai-no-Katasumi-ni)/
I want to make my url like this, I do'nt want ( or ) or : etc in my link.
http://localhost/piratefiles/view/2/in-this-corner-of-the-world-kono-sekai-no-katsumi-ni
or just
http://localhost/piratefiles/view/2/
my code
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} "^([^/]*?)(?:/|%20)+([^/]+?(?:/|%20)+.*)$"
RewriteRule ^ %{REQUEST_URI}?%1-%2 [N,NE]
RewriteCond %{QUERY_STRING} "^([^/]*?)(?:/|%20)+([^/]+?)/?$"
RewriteRule ^ %{REQUEST_URI}?%1-%2/ [L,R=302,NE]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/$ index.php [L]
#Your section
RewriteRule ^view/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ view.php?idp=$1&cat=$2&pst=$3 [NC,L]
ErrorDocument 404 http://www.akshadainfosystem.com/
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Put this code in your .htaccess file and clear all cookie

.htaccess Rewrite rules causing empty $_GET

I am having a problem where on some of my clean url pages the $_GET values are returning as null.
My URL rewrites:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ /$1 [L,R=301] !facebookexternalhit
RewriteCond %{HTTP_HOST} ^codesmite.com [NC] !facebookexternalhit
RewriteRule ^(.*)$ http://www.codesmite.com/$1 [L,R=301,NC,QSA]
# internally add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,QSA]
RewriteRule ^([0-9]+)/?$ index2.php?section=index&page=$1 [NC,QSA]
RewriteRule ^freebies/?$ index2.php?section=freebies&page=1 [L,NC,QSA]
RewriteRule ^articles/?$ index2.php?section=articles&page=1 [L,NC,QSA]
RewriteRule ^deals/?$ index2.php?section=deals&page=1 [L,QSA,NC]
RewriteRule ^freebies/([0-9]+)/?$ index2.php?section=freebies&page=$1 [NC,L,QSA]
RewriteRule ^articles/([0-9]+)/?$ index2.php?section=articles&page=$1 [NC,L,QSA]
RewriteRule ^deals/([0-9]+)/?$ index2.php?section=deals&page=$1 [NC,L,QSA]
RewriteRule ^article/(.*)$ article.php?stitle=$1 [NC,L,QSA]
RewriteRule ^deal/(.*)$ deal.php?stitle=$1 [NC,L,QSA]
RewriteRule ^freebie/(.*)$ freebie.php?stitle=$1 [NC,L,QSA]
RewriteRule ^legal/?$ legal.php [NC,L,QSA]
The pages I am having problems with are:
www.example.com/articles/
www.example.com/freebies/
www.example.com/deals/
I am trying to set a value on these pages based on $_GET['section'] but $_GET array is null.
There are also folders called "articles", "freebies" and "deals" located in the root directory (I do not know if this makes any difference to my problems).
What am I doing wrong?
Nevermind, as soon as I posted this question I noticed my mistake, forgot to change index2 to index from my test file to live file.
No problem with my code, silly mistake ^_^.

How to improve this .htaccess file?

I created the following .htaccess file and I am wondering if it could be improved:
RewriteEngine On
RewriteRule ^site$ ./site/ [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
I want to redirect visitors which enter www.domain.com/site/ to www.domain.com/main/ and all other requests to www.domain.com/404.php?url=... This works, except when the user doesn't enter a trailing slash (www.domain.com/site).
You should be able to do that with a /? like
RewriteEngine On
RewriteRule ^site$ ./site/? [L,NC]
RewriteRule ^site/(.*)$ ./main/$1 [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/en [NC]
RewriteRule ^(.*)$ ./404.php?url=$1 [L,NC]
For the first rule, you rewrite site to site/ and then rewrite site/ to main/. I would rather do it in one step, e.g.
RewriteRule ^site/?$ ./main/ [L,NC]
The second rule would stay as it is.
With the current rules, you only redirect nonexisting paths to 404.php. If this is what you want, everything is fine. If not, you should remove !-f and !-d conditions and add a condition to exclude main from being rewritten to 404
RewriteCond %{REQUEST_URI} !/main/ [NC]
Even though the substitution ./404.php is valid, I would write it as
RewriteRule ^(.*)$ 404.php?url=$1 [L,NC]

Using htaccess to rewrite url with variables?

Using htaccess how can instead of the following to have something like
http://domain.com/verify
or
http://domain.com/verify?user=_FIRST-VARIABLE_&verification_code=_SECOND-VARIABLE_
Here is the original
http://domain.com/account/index.php?user=_FIRST-VARIABLE_&verification_code=_SECOND-VARIABLE_
My current htaccess is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^login$ account/index.php [NC]
RewriteRule ^register$ account/index.php?register [NC]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try this RewriteRule with the regex pattern
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^varify/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$/account/index.php?foo=$1&bar=$2 [NC,QSA,L]
change the variables foo and bar with your variables.
All requests to example.com/varify/var1/var2 will be redirected to example.com/account/index.php?foo=$1&bar=$2

Categories