• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 695
  • Last Modified:

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

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
joomla_php
Asked:
joomla_php
  • 3
  • 2
1 Solution
 
Shinesh PremrajanCommented:

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

Hope this helps
0
 
joomla_phpAuthor Commented:
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
 
M. Rashel AhmedCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
joomla_phpAuthor Commented:
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
 
M. Rashel AhmedCommented:
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
 
joomla_phpAuthor Commented:
Don't know if this works, needed follow up but didn't get it.
0

Featured Post

Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now