Rewrite rule for replacing a string of characters isn't work

New to mod_rewrite? This is a good place to start.

Rewrite rule for replacing a string of characters isn't work

Postby MAD » Fri Sep 04, 2009 11:51 pm

My old SEF program on my Joomla site used to change the (partial string) "name - name" in my URL into "name---name" (with 3 hyphens) replacing all spaces with hyphens. Since this program isn't suported anymore I got another program but it's smarter and changes it into "name-name" (with only 1 hyphen), automatically deleting the extra two. I think the last is better, but now it's raining 404's and almost all my backlinks aren't working anymore. I'd like to make a Rewrite rule replacing all characters "---" for "-" used in folders, subfolder and filenames. I came up with this:

Code: Select all
RewriteCond %{QUERY_STRING} ^([^---]+) [NC]
Rewriterule ^(.*)\---(.*)$ http://www.mywebsite.com/$1-$2 [R=301,L]


but it isn't working yet (and when right will this work on subfolder names and filenames too?) Any tips are apreciated.
MAD
 
Posts: 5
Joined: Fri Sep 04, 2009 2:02 am

Postby richardk » Sat Sep 05, 2009 8:22 am

Do you have URLs with more than one occurrence of ---?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^-]*)-{2,}([^-].*)?$ /$1-$2 [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby MAD » Sat Sep 05, 2009 9:19 am

Thanks for your reply. As far as I can see it only happens once per URL, though it's sometimes in a filename, sometime in a sectionname (=subfolder name).

Your suggestion didn't work. Does it matter where it's in the htaccess, since the same htaccess is also creating the SEF from the Joomla file name? I now placed it at the end. My htaccess looks like this in full now:

Code: Select all
##
# @version $Id: htaccess.txt 5975 2006-12-11 01:26:33Z robs $
# @package Joomla
# @copyright Copyright (C) 2005 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.
#
# Only use one of the two SEF sections that follow.  Lines that can be uncommented
# (and thus used) have only one #.  Lines with two #'s should not be uncommented
# In the section that you don't use, all lines should start with #
#
# For Standard SEF, use the standard SEF section.  You can comment out
# all of the RewriteCond lines and reduce your server's load if you
# don't have directories in your root named 'component' or 'content'
#
# If you are using a 3rd Party SEF or the Core SEF solution
# uncomment all of the lines in the '3rd Party or Core SEF' section
#
#####################################################

#####  SOLVING PROBLEMS WITH COMPONENT URL's that don't work #####
# SPECIAL NOTE FOR SMF USERS WHEN SMF IS INTEGRATED AND BRIDGED
# OR ANY SITUATION WHERE A COMPONENT's URL's AREN't WORKING
#
# In both the 'Standard SEF', and '3rd Party or Core SEF' sections the line:
# RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR] ##optional - see notes##
# May need to be uncommented.  If you are running your Joomla!/Mambo from
# a subdirectory the name of the subdirectory will need to be inserted into this
# line.  For example, if your Joomla!/Mambo is in a subdirectory called '/test/',
# change this:
# RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR] ##optional - see notes##
# to this:
# RewriteCond %{REQUEST_URI} ^(/test/component/option,com) [NC,OR] ##optional - see notes##
#
#####################################################



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

#
#  mod_rewrite in use

RewriteEngine On


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

# RewriteBase /


########## Begin - Joomla! core SEF Section
############# Use this section if using ONLY Joomla! core SEF
## ALL (RewriteCond) lines in this section are only required if you actually
## have directories named 'content' or 'component' on your server
## If you do not have directories with these names, comment them out.
#
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR]       ##optional - see notes##
#RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
#RewriteRule ^(content/|component/) index.php
#
########## End - Joomla! core SEF Section



########## Begin - 3rd Party SEF Section
############# Use this section if you are using a 3rd party (Non Joomla! core) SEF extension - e.g. OpenSEF, 404_SEF, 404SEFx, SEF Advance, etc
#
RewriteCond %{REQUEST_URI} ^(/component/option,com) [NC,OR]       ##optional - see notes##
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|/[^.]*)$  [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
#
########## End - 3rd Party SEF Section



########## 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!
#
# Block out any script trying to set a mosConfig value through the URL
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

RewriteRule ^([^-]*)-{2,}([^-].*)?$ /$1-$2 [R=301,L]


Don't I need a RewriteCond since it's all external links coming in and all my internal pages are already named correct with one hyphen? Or am I talking nonsense now?
MAD
 
Posts: 5
Joined: Fri Sep 04, 2009 2:02 am

Postby richardk » Sun Sep 06, 2009 3:22 pm

It has to go first. Put it after
Code: Select all
RewriteEngine On
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby MAD » Sun Sep 06, 2009 3:33 pm

Nope, still not rewriting. See:

http://www.galactica.tv/battlestar-galactica-1978---interviews/noah-hathaway-galactica.tv-interview.html

While this page does exist (note the 3 hyphens after 1978 in the link above, compared to the one in the link below):

http://www.galactica.tv/battlestar-galactica-1978-interviews/noah-hathaway-galactica.tv-interview.html
MAD
 
Posts: 5
Joined: Fri Sep 04, 2009 2:02 am

Postby MAD » Sun Sep 06, 2009 3:45 pm

It does work on this one:

http://www.galactica.tv/main/galactica.tv---site-objective.html

but it doesn't work on this one:

http://www.galactica.tv/battlestar-galactica-2003/battlestar-galactica-2003---crew.html

It probably has to do with the place the triple hyphen is, right? Whether there a single hyphen first before the triple one?
MAD
 
Posts: 5
Joined: Fri Sep 04, 2009 2:02 am

Postby richardk » Sun Sep 06, 2009 3:48 pm

Doh, replace
Code: Select all
^([^-]*)-{2,}([^-].*)?$

with
Code: Select all
^(.*[^-])?-{2,}([^-].*)?$
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby MAD » Sun Sep 06, 2009 3:52 pm

That did the trick! Thanks a million :D :D :D
MAD
 
Posts: 5
Joined: Fri Sep 04, 2009 2:02 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 31 guests

cron