I'm working in a directory called testsite and I want all .php extensions to be replaced by a trailing slash. I'm halfway there, in that (for example) entering http://mydomain.com/testsite/about means that http://mydomain.com/testsite/about.php is loaded.
However, I now want the URL to be displayed as ./about/ so that only one version shows up in search engine rankings.
Here's my .htaccess:
RewriteEngine On
RewriteBase /testsite/
RewriteRule ^()$ index.php [NC,L]
RewriteCond %{REQUEST_URI} !(^/?.*\..*$) [NC]
RewriteRule (.*)$ $1.php [NC]
Also, is it possible to preserve (and hide from display any parameters that I pass)?
All help is much appreciated!
Try these rules:
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteRule ^$ index.php [L]
RewriteRule (.*)/$ $1.php [NC]
Try replacing the last rule with:
RewriteRule (.*)$ $1.php [NC] [E=ORIGINAL_URL:$1]
Related
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 ^_^.
I am trying to clean up the URLs on my website.
My structure is currently
lifestrology.com/113-matt-barnes.html
But I need it to be:
lifestrology.com/title-of-gallery/name-of-photo
My current rewrite structure is:
Options +FollowSymlinks
RewriteEngine on
#Please edit this part to match your website
#RewriteCond %{HTTP_HOST} ^lifestrology.com/$
#RewriteRule (.*) http://lifestrology.com/$1 [R=301,L]
#Please do not edit anything underneath.
RewriteCond %{QUERY_STRING} msg=(.+)
RewriteRule ^(.+)\.html?$ view.php?id=$1&msg=%1 [QSA,L]
RewriteRule ^(.+)\.html?$ view.php?id=$1 [NC]
Can someone help me with fixing the structure of my urls to make them lifestrology.com/title-of-gallery/name-of-photo ? Also can this be fixed to work with the current items already on my website?
Try this rule:
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
and for any additional slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
after tailoring the code above to suit your custom needs.
Hope it helps.
I want to rewrite url on my site such that,
www.example.com/login points to www.example.com/login.php
www.example.com/login/ too points to www.example.com/login.php
Pages deep inside the site should follow the above rule. i.e
www.example.com/inner/inner2/something
and www.example.com/inner/inner2/something/ should also point to
www.example.com/inner/inner2/inner3/something.php
www.example.com/login/anything , if doesn't exist should show 404 (as in my case it's causing loops)
The rule I have written is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*[^/])$ $1.php [L]
RewriteRule ^(.+)/$ $1.php [L]
It works fine, in 3 of the above cases but in 4th it's causing loops.
Also I want to show an 404, if a file with .php extension is requested.
And also, the behaviour of above rule changes if I use REQUEST_URI instead of REQUEST_FILENAME. What's the difference between the two?
TIA
Try this rule for php extension hiding:
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
I have files in my root folder. jobs.php, contact.php.
I want to be able to use .htaccess to remove the .php extension and force a trailing slash /. So that, www.domain.com/jobs/ and www.domain.com/contact/ goes to their respective pages.
Also, I want jobs.domain.com to point to www.domain.com/jobs/
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
RewriteRule ^(rosquillos)/$ $1 [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
</IfModule>
This is what I've got so far, and as much as I try. I can't seem to get it to work.
Any help would be awesome.
EDIT:
I would like to rephrase my question. The existing code removes the .php extensions for me, what I really only need right now is an additional rule that points the subdomain to the correct file: ie. jobs.domain.com points to jobs.php. I can live without the trailing slash.
Your condition:
RewriteCond %{REQUEST_FILENAME}\.php -f
will fail if your URL's end with a trailing slash. But if you only have jobs and contact then you'll just want:
RewriteEngine On
# add trailing slash and redirect browser
RewriteRule ^([^/]+)$ /$1/ [L,R=301]
RewriteRule ^([^/]+)/$ /$1.php [L]
# redirect direct access to php files:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+)\.php
RewriteRule ^ /%1/ [L,R=301]
Oh and I forgot:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^ /%1.php [L]
Assuming that *.domain.com has the same document root as www.domain.com.
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]