homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Rewrite rule for url redirect

 8:55 pm on Dec 5, 2010 (gmt 0)

I need to place a rule to my htaccess file to redirect the old url to the new one.
I have:
http://www.example.com/albums/bongo/album36/104_0411_IMG.jpg.html (old url)
and I need to remove the .jpg.html from the url, so it looks like:
http://www.example.com/albums/bongo/album36/104_0411_IMG (new url)

I appreciate it if someone can show me the redirect rule for the htaccess file.



 9:56 pm on Dec 5, 2010 (gmt 0)

RewriteRule ^([^.]+)\.jpg.html$ http://www.example.com/$1 [R=301,L]

Beware that requests for images without an extension within the URL will fail in some browsers.


 10:39 pm on Dec 5, 2010 (gmt 0)

Thanks, but it is not redirecting. I place the rule at the htaccess file but nothing happens. What am I doing wrong? below is the htaccess file.

<IfModule mod_php5.c>
php_flag short_open_tag On
php_flag magic_quotes_gpc Off
php_flag magic_quotes_sybase Off
php_flag magic_quotes_runtime Off
php_flag register_globals Off
php_flag session.auto_start Off
php_flag suhosin.session.encrypt Off
php_value upload_max_filesize 20M
php_value post_max_size 100M

# Try to disable the parts of mod_security that interfere with the Flash uploader
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off

# Increase security by uncommenting this block. It keeps browsers
# from seeing support files that they shouldn't have access to. We
# comment this out because Apache2 requires some minor configuration
# in order for you to use it. You must specify "AllowOverride Limit"
# in your Apache2 config file before you uncomment this block or
# you'll get an "Internal Server Error".
# <FilesMatch "(\.(class|fla|gitignore|inc|ini|sql|txt)|(README|LICENSE))$">
# Order deny,allow
# Deny from all
# </FilesMatch>
# <FilesMatch "robots.txt">
# Order allow,deny
# Allow from all
# </FilesMatch>

# Improve performance by uncommenting this block. It tells the
# browser that your images don't change very often so it won't keep
# asking for them. If you get an error after uncommenting this, make
# sure you specify "AuthConfig Indexes" in your Apache config file.
# <IfModule mod_expires.c>
# ExpiresActive On
# # Cache all files for a month after access (A).
# ExpiresDefault A2678400
# # Do not cache dynamically generated pages.
# ExpiresByType text/html A1
# </IfModule>

# You can use the mod_rewrite Apache module to get rid of the
# "index.php" from your Gallery 3 urls. Uncomment the block below
# inside the <IfModule> ... </IfModule> lines and then edit the
# RewriteBase line to match your Gallery 3 URL.
# Here are some RewriteBase values:
# Gallery 3 URL RewriteBase line
# ============= ====================
# http://example.com/gallery3 RewriteBase /gallery3
# http://example.com/~bob/photos RewriteBase /~bob/photos
# http://gallery3.example.com/ RewriteBase /
# Then just use your Gallery 3 without the index.php in the url.
# NOTE: future upgrades of Gallery 3 will overwrite this file! If you
# want these changes to be persistent, talk to your system admin about
# putting this block into your Apache config files.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /albums
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]
RewriteRule ^index.php/(.*) $1 [QSA,R,L]

RewriteRule ^([^.]+)\.jpg.html$ http://www.example.com/$1 [R=301,L]


 11:07 pm on Dec 5, 2010 (gmt 0)

I missed the escaping on the second period in the pattern.
RewriteRule ^([^.]+)\.jpg\.html$ http://www.example.com/$1 [R=301,L]

The pattern relies on the first period in the URL being that immediately before the "jpg". If there are other periods, the pattern needs to be amended.

External Redirects must always be placed before any and all Internal Rewrites, but after the
RewriteEngine On directive.

Your rules within the <ifmodule> container are also in the wrong order and far from optimum.

The redirect is last, but should be first. The R flag generates a 302 redirect, but you need a 301 redirect. The redirect should also contain the domain name in the target.

The -d and -f "exists" checks are very inefficient and run for every request arriving at your server: images, scripts, stylesheets, everything. You should precede the "exists" checks with a negative match RewriteCond that excludes all requests that never need to be checked, something like
RewriteCond %{REQUEST_URI} \.(png|j(pe?g|s)|gif|css)$ or somesuch. This rewrite code should be last; after the redirects.

I don't understand why you are using the [PT] flag here.

One of your rules has the [QSA] flag, but does not need it, as appending the query string happens automatically if no other query string has been specified.


 5:30 pm on Dec 6, 2010 (gmt 0)

Thank you, works perfect, I placed the rule at the top of the htaccess file. I also set the
RewriteEngine on directive as it was missing.

RewriteEngine on
RewriteRule ^([^.]+)\.jpg\.html$ http://www.example.com/albums/$1 [R=301,L]

I appreciate the feedback, but this file comes straight from the Gallery3 (Menalto) installation package.
I haven't modify anything, as I do not have the knowledge to do so. Therefor I can not comment on your remarks and recommendations.

I will set your feedback if you don't mind to the Gallery3 forum. Maybe a developer will pick it up and apply your suggestions.


 6:48 pm on Dec 6, 2010 (gmt 0)


[edited by: g1smd at 6:54 pm (utc) on Dec 6, 2010]


 6:53 pm on Dec 6, 2010 (gmt 0)

These sorts of errors are common.

The default installs for Wordpress, Joomla, Drupal, and very many others have these issues within.

I had a quick look for the SVN repository for this code and didn't find it.

Perhaps you could start a new thread with a copy of the default .htaccess file for discussion (if their licence permits it).


 11:43 am on Dec 8, 2010 (gmt 0)

It appears that the G3 developers are very interested for your suggestions and requested me to create a ticket.

here is the topic I started at gallery3 regarding my issue with your feedback (I haven't created a new one specifically for the whole htaccess file)

I quote the comment of the G3 creator on this ticket below:

Awesome, thanks for the info. I'm totally not a rewrite expert, but it sounds like you are! Would you do me a favor and take a shot at rewriting that entire block? I'll be happy to replace what we have with what you suggest for 3.0.1

But of course I'm not the expert here. That is you. If its not a lot of pain for you It will be appreciated if you have any more additions/corrections to the file. you may comment straight at the sourceforge ticket if you like.


 2:35 pm on Dec 8, 2010 (gmt 0)

I would suggest incorporating the following tweaks:

# Order deny,allow
# <FilesMatch "(\.(class|fla|gitignore|inc|ini|sql|txt)|(README|LICENSE))$">
# Deny from all
# </FilesMatch>
# <FilesMatch "robots\.txt$">
# Allow from all
# </FilesMatch>
# ...
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /albums
# External redirects must precede internal rewrites to avoid exposing internal filepaths
# to the client as URLs. Literal periods in regular-expressions patterns must be escaped.
# Externally redirect requests for URL-path "/index.php/<anything or nothing>" to "/<anything or nothing>"
RewriteRule ^index\.php/(.*)$ http://www.example.com/$1 [R=301,L]
# Additional recommended rule: Externally redirect direct client requests for script filepath to index URL
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*index\.php([?#][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
# Additional recommended rule: Externally redirect all non-blank
# non-canonical hostname requests to canonical hostname
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Excluding all frequently-requested filetypes that are NOT handled by the "/index/php" script
RewriteCond $1 !\.(php|css|js|xml|txt|doc|pdf|avi|mov|wmv|mp3)$
# And skipping the rule if the requested URL-path resolves to a physically-existing file or directory
# (these checks are potentially very slow because they may require a physical disk read)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Internally rewrite all other requested URL-paths to /index.php script filepath with query string
RewriteRule ^(.*)$ index.php?kohana_uri=$1 [QSA,PT,L]
# Rewrite directory index requests to /index.php script
RewriteRule ^$ index.php?kohana_uri=$1 [QSA,PT,L]

Note that because I assume that the purpose of this script is to serve images, I did not exclude image filetypes from the file- and directory-exists checks as I would usually do, for example, in the case of forum or blog scripts.

Note that I removed all of the <IfModule> containers because their purpose is to avoid a 500-Server Error and cause the server to fail silently in cases where the required Apache module is not available. But in almost all cases, users of scripts such as this will want to know that the module is not available; otherwise they will waste an awful lot of time trying to fiugre out why this code won't work. Therefore, getting a 500-Server error if a module is missing would be a Good Thing(TM).

It is unnecessary (and wasteful) to use the [QSA] flag on rules which do not append new/additional name/value pairs to an existing query string. If no existing query string is expected/possible/desired on the requested URL-path, or if no new/additional name/value pairs are to be appended to that existing query string, then don't include [QSA].

It is not necessary to exclude all known filetypes from the file- and directory-exists checks; only the most-frequently requested filetypes need to be excluded to get a tangible performance increase by skipping these checks and the rule.

These recommendations are general, and will need to be thoroughly evaluated, understood, tested, and clearly documented by the script author. I do not use this script, and may indeed have "missed something" about how it is intended to work.



 6:51 am on Dec 9, 2010 (gmt 0)

Thanks for your help. Gallery 3 project developer notified.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved