Solved

.htaccess 301 redirect to get rid of index.php and .html?

Posted on 2010-11-22
6
593 Views
Last Modified: 2013-11-19
Is there a way to rewrite/redirect a URL of the following pattern:

http://www.xyz.com/index.php/2010/10/14/56-long-url-here.html

to direct it to the same URL but without index.php and .html?

http://www.xyz.com/2010/10/14/56-long-url-here

Google has the first pattern cached but the site gives 404's because it is serving the second pattern.

So we want any Google clicks to go to the right page rather than to the 404.
0
Comment
Question by:joomla_php
  • 3
  • 2
6 Comments
 
LVL 17

Accepted Solution

by:
shinuq earned 500 total points
ID: 34194667

RewriteCond   %{HTTP_HOST}   ^www.xyz.com$   [NC]
RewriteRule   ^index.php/([A-Za-z0-9-]+)/([0-9]+)/?$   $1 [L]

Hope this helps
0
 
LVL 13

Author Comment

by:joomla_php
ID: 34194737
it had no effect, perhaps because the site already has these directives?

##
# @version $Id: htaccess.txt 13415 2009-11-03 15:53:25Z ian $
# @package Joomla
# @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
# @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
# Joomla! is Free Software
##


#####################################################
#  READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations.  It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that dissallows changing it in
# your .htaccess file.  If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's.  If they work,
# it has been set by your server administrator and you do not need it set here.
#
#####################################################

##  Can be commented out if causes errors, see notes above.
Options +FollowSymLinks

#
#  mod_rewrite in use

RewriteEngine On

########## Begin - Rewrite rules to block out some common exploits
## If you experience problems on your site block out the operations listed below
## This attempts to block the most common type of exploit `attempts` to Joomla!
#
## Deny access to extension xml files (uncomment out to activate)
#<Files ~ "\.xml$">
#Order allow,deny
#Deny from all
#Satisfy all
#</Files>
## End of deny access to extension xml files
RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
# Block out any script trying to base64_encode crap to send via URL
RewriteCond %{QUERY_STRING} base64_encode.*\(.*\) [OR]
# Block out any script that includes a <script> tag in URL
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Send all blocked request to homepage with 403 Forbidden error!
RewriteRule ^(.*)$ index.php [F,L]
#
########## End - Rewrite rules to block out some common exploits

#  Uncomment following line if your webserver's URL
#  is not directly related to physical file paths.
#  Update Your Joomla! Directory (just / for root)

RewriteBase /


########## Begin - Joomla! core SEF Section
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$  [NC]
RewriteRule (.*) index.php
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
#
########## End - Joomla! core SEF Section

Open in new window

0
 
LVL 6

Expert Comment

by:M. Rashel Ahmed
ID: 34279335
Hello,

You can follow those steps:

How to implement the 301 Redirect

1. To create a .htaccess file, open notepad, name and save
the file as .htaccess (there is no extension).

2. If you already have a .htaccess file on your server,
download it to your desktop for editing.

3. Place this code in your .htaccess file:

redirect 301 /old/old.htm http://www.you.com/new.htm

4. If the .htaccess file already has lines of code in it,
skip a line, then add the above code.

5. Save the .htaccess file

6. Upload this file to the root folder of your server.

7. Test it by typing in the old address to the page you've
changed. You should be immediately taken to the new
location.

Notes: Don't add "http://www" to the first part of the
statement - place the path from the top level of your site
to the page. Also ensure that you leave a single space
between these elements:

redirect 301 (the instruction that the page has moved)

/old/old.htm (the original folder path and file name)

http://www.you.com/new.htm (new path and file name)

When the search engines spider your site again they will
follow the rule you have created in your .htaccess file.
The search engine spider doesn't actually read the
.htaccess file, but recognizes the response from the
server as valid.

During the next update, the old file name and path will be
dropped and replaced with the new one. Sometimes you may
see alternating old/new file names during the transition
period, plus some fluctuations in rankings. According to
Google it will take 6-8 weeks to see the changes reflected
on your pages.


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 13

Author Comment

by:joomla_php
ID: 34279424
That's excellent, I had really wanted a rewrite and not a redirect though. I was aware of the redirect I just didn't want to do it for hundreds of links. It's not old.html to new.html, it's old.html to old. Very big difference. but thank you.
0
 
LVL 6

Expert Comment

by:M. Rashel Ahmed
ID: 34279768
ok, i think that exapmle will help you to figure it out:

The following code converts: http://yourdomain.com/tutorials.php?req=tutorial&id=3&page=0 to http://yourdomain.com/tutorials/3/0.php:

Code:
RewriteEngine On
RewriteRule ^tutorials/(.*)/(.*).php /tutorials.php?req=tutorial&tut_id=$1&page=$2


** You need to modify the code so that it doesn't show the .php extension.

0
 
LVL 13

Author Closing Comment

by:joomla_php
ID: 34328173
Don't know if this works, needed follow up but didn't get it.
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

If you've heard about htaccess and it sounds like it does what you want, but you're not sure how it works... well, you're in the right place. Read on. Some Basics #1. It's a file and its filename is .htaccess (yes, with a dot in the front). #…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now