htacces redirection if the url doesn't have .php extension - .htaccess

I wish to redirect all page requests if
(1) Current URL doesn't have .php extension
(2) Current Page is not the Site Home (ie www.site.com )
Example:
http://site.com/robin to profile.php?screen_name=robin
Can someone suggests the htaccess to accomplish this?

This site should give some hints:
http://www.webweaver.nu/html-tips/web-redirection.shtml
an excerpt from there:
Changed file extension?
This example is perfect if you've
decided to switch to .php from .html
pages. It will look for any .html page
and redirect it to .php (ie
http://www.example.com/yourpage.html
and redirect it to
http://www.example.com/yourpage.php).
Now, be careful with this, it does
mean any html page. I did this on one
of my sites and had totally forgotten
I had an iframe with .html content on
some pages... I didn't notice for
weeks that it was broken :S. So learn
from my mistake ;-) check, double
check, then check again.
RedirectMatch 301 (.*)\.html$ http://www.example.com$1.php
You can put arbitrary regular expressions as parameter to Redirectmatch, so you need to add a regular expression which matches '".php" not last of line'. I am only a neophyte of RegExes myelf, so i do not provide an example yet. Check for a later update -have to do some experimating.

Related

Remove subfolder using htaccess

I know that this has been requested a million times but i haven't found an answer.
Basically I want to remove amp from my site but I've a million pages indexed using /amp so if I remove it this will cause a mess.
What I want to do is to redirect all the url like these
https://*/amp
will go to the same url without amp.
For example an article with this url
https://www.tuttosullapostaelettronica.it/blog/come-e-fatta-una-mail/amp
will be redirected to the same article without /amp like
https://www.tuttosullapostaelettronica.it/blog/come-e-fatta-una-mail
This has to be valid for whatever article url that end in /amp that has to be the /amp removed
Thanks!
Assuming you need to remove amp on requests to the document (first example) and /amp on requests to other URLs then you would need a rule like the following using mod_rewrite, near the top of the .htaccess file:
RewriteEngine On
RewriteRule ^(?:(.+)/)?amp$ /$1 [R=301,L]
The regex ^(?:(.+)/)?amp$ matches both amp and <something>/amp and removes amp or /amp respectively.
Test first with a 302 (temporary) redirect to avoid potential caching issues.

htaccess redirect with folder name

I need some help with htaccess redirects. Out site was wrongly crawled by google and as a result there are a lot of wrong urls being shown in webmaster tool. As an example:
articles/abcd/xyz
should be redirected as
articles/abcd/
articles/abcd/xyz.php
should be redirected as
articles/abcd/
articles/abcd/xyz.html
should be redirected as
articles/abcd/
So basically I am trying to mean always redirect to articles/abcd/ for varous wrong url types that i shown above. Please could you help
You can simply use RedirectMatch from mod_alias (documentation). We assume that the part after articles does not contain any / character. We redirect with a temporary redirect to the url without the suffix. Change "temp" to "permanent" after testing that this redirect actually works as you expect it to work.
RedirectMatch temp ^(/articles/[^/]+/).+$ $1

The trailing slash in home page

I will be really greatful if someone helps me with this.
Let's consider these 2 URLs (both returning 200 in the response header):
www.foo.com/something
www.foo.com/something/
Google considers these 2 URLs different despite both having the same content which leads to a duplicated content problem. To solve the issue it is advised to either use the 301 permanent redirect to redirect one URL to the other or use the rel="canonical" attribute. source
Wordpress blogs deal perfectly with this matter. When adding the trailing slash to my internal links, I was redirected to URLs without the trailing slash (301 response).
The problem is the redirect is only happening with internal pages. My homepage seem to return a 200 response with or without the trailing slash. Should I leave it as it is or force a redirect with the .htaccess file?
p.s.: The backlinks to my website have 2 different hrefs (with and without the trailing slash). Should I change those backlinks to a unique href or redirect one to the other?
Use this link to add trailing slash to end of your url
It doesn't matter whether your Backlinks are with slash or not, because after implementing techniques mentioned in above address, search engines will assume your pages only with slashes. Remember because of past indexing you should wait until former index to be deleted. or you may use 301 redirect to pages with slash. Basically this will take some time until search engines came again and find your redirect rules, too... .
By home page I assume you mean the page shown when you enter just the domain.
With or without the slash represents exactly the same URL. Nothing to worry about and nothing you can do.

using mod_rewrite to create SEO friendly URLS

I've been searching google for this but can't find the solution to my exact needs. Basically I've already got my URL's named how I like them i.e. "http://mysite.com/blog/page1.php"
What I'm trying to achieve (if it's possible!) is to use rewrite to alter the existing URLS to: "http://mysite.com/blog/page1"
The problem I've come across is I've found examples that will do this if the user enters "http://mysite.com/blog/page1" into the broweser which is great, however I need it to work for the existing links in google as not to loose traffic, so incoming URLS "http://mysite.com/blog/page1.php" are directed to "http://mysite.com/blog/page1".
The 1st example (Canonical URLs) at the following is pretty much what you want:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
This should do the trick, rewriting requests without .php to have it, invisible to the user.
RewriteEngine On
RewriteRule ^/blog/([^.]+)$ /blog/$1.php
You will need to write a rewrite rule for mapping your old url's to your new url as a permanent redirect. This will let the search engine know that the new, seo friendly url's are the ones to be used.
RewriteRule blog/page1.php blog/page1 [R=301,L]

how to use htaccess to chance the URL...?

I googled and looked on stackover flow but i failed to really understand the answers
I;m not trying to REDIRECT but CHANGE the WAY the URLS LOOK
I want to change a these into the new urls
www.site.com/abc.php to www.site.com/
(my index page currently has my login page so I can't use the index.php)
www.site.com/abc.php#123.php to www.site.com/123.php
UPDATE:
Ok, how can I do this then
www.site.com/abc.php to www.site.com/abc/
so that when a hash link is present it looks like
www.site.com/abc/#123.pho
or if possible
www.site.com/abc/#123/
In addition to the other answer on redirects, if you want more advanced forwarding you can use the Apache RewriteEngine module.
You can then use regexps, which may include subpatterns.
Example from my site, I created the patterns after I've imported everything from blogger to wordpress. Whenever someone visits an URL like http://www.twistedmind.nu/2006_03_01_archive.html he'd be redirected to http://twistedmind.nu/2006/03
RewriteEngine on
RedirectMatch 301 (([0-9]*)_([0-9]*)_([0-9])(.)(.html))$ http://twistedmind.nu/$2/$3/
RedirectMatch 301 (([0-9])(.)(.html))$ http://twistedmind.nu/$1
Based on the other answer, you can't match on everything that's after the hash tag # though.
Other example (added after comment):
RedirectMatch 301 (.*).php$ http://www.mysite.com/$1
This should strip the .php extension from all links, the new link (withouth .php) should exist.
You can use mod_rewrite if you want to create 'virtual' urls that redirect something like mysample to mysample.php. See http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html for an explanation.
You can use an .htaccess only for the first url.
Redirect /abc.php http://www.site.com/
The second url cannot be redirected with the .htaccess.
You can use javascript for that:
<script type="text/javascript">
if (location.hash == "123")
location.href = location.hash+".php";
</script>

Resources