Quick Mod_Rewrite Question
Posted November 22nd, 2005 @ 01:17pm by Erik J. Barzeski
If I have a URL like this:
http://url.com/sub/direc/file.php
And I want to redirect users to:
http://url.com/sub/folder/file.php
(swapping out /direc/ for /folder/), how would I write the mod_rewrite rule? I really should learn regular expressions one of these days.
Posted 22 Nov 2005 at 1:46pm #
Add the following to your Apache config in the correct area:
RewriteEngine on
RewriteRule /sub/direc/(.*) http://url.com/sub/folder/$1
This is a basic example. Check out http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html for the full list.
Posted 22 Nov 2005 at 1:48pm #
How about:
RewriteEngine on
RewriteCond %{REQUEST_URI} "/sub/.*/file.php"
RewriteRule ^/sub/.*/file.php$ /sub/folder/file.php
I don't think there's any regex beyond .* in it.
Posted 22 Nov 2005 at 2:05pm #
Justin, I want to redirect all items in that folder, not just things named "file.php." There may be 300 files in there.
Eric, at first glance, your rule doesn't seem to be having much effect at all. Is it missing a $ or something?
FWIW, the only current rewrite rule I have is this:
RewriteRule ^(.*)\.golf$ http://thesandtrap.com/$1.php [R=permanent]
It takes any links to articles with .golf and sends them to the .php article. We had, for awhile, ".golf" extensions at The Sand Trap.
Posted 22 Nov 2005 at 2:40pm #
RewriteEngine on
RewriteRule /sub/direc/(.*) /sub/folder/$1
Similar to Eric W, but you don't need the full URL in the second part (you do for Redirects, not Rewrites)
Posted 22 Nov 2005 at 3:48pm #
For some reason, it's not working. Here are the actual URLs:
http://thesandtrap.com/archives/international/tiger_defends_dunlop_defeats_duval.php should point to http://thesandtrap.com/archives/other/tiger_defends_dunlop_defeats_duval.php (this is true for any and all articles previously in the "international" directory.
My .htaccess file currently reads:
RewriteEngine On
RewriteRule ^(.*)\.golf$ http://thesandtrap.com/$1.php [R=permanent]
RewriteRule /archives/international/(.*) /archives/other/$1
As you can see, the ".golf" redirect works fine. Currently, none of the versions listed above work. I've changed the order, commented out the .golf rewrite, and done other things. None of the variations I've tried have worked either.
Posted 22 Nov 2005 at 4:06pm #
Erik,
The redirecting is actually taking place, but only on the server side. The URL doesn't change as far as the browser's concerned.
Try doing this:
RewriteRule /archives/international/(.*) /archives/other/$1 [R=seeother]
That will alter the redirect type so the URL will change on the browser side.
Posted 22 Nov 2005 at 4:14pm #
Andrew, except that it isn't happening. The "/other/" link displays a result. The "/international/" URL shows search results, which means that the request generated a 404 response.
Your line of code is in there now, and nothing happens. Just the 404 search.
Posted 22 Nov 2005 at 4:31pm #
Sanity check -- is this in an .htaccess file, and if so, is it at the top of the site, i.e. not in the subdir outside of /archives so that /archives never sees it?
Posted 22 Nov 2005 at 4:40pm #
In a .htaccess rewrite, everything is relative to the current directory, so I think the initial '/' is what's killing the rule. Try:
RewriteRule ^archives/international/(.*)$ /archives/other/$1
Posted 22 Nov 2005 at 5:37pm #
Stuart got it! Thanks Stuart. I appreciate it! (Yes, Justin, the .htaccess file is at the site's root.)
Posted 25 Nov 2005 at 3:56am #
You could do this with RedirectMatch, no need for a rewrite rule:
RedirectMatch /archives/international/(.*) http://blah.com/archives/other/$1