Subscribe to
Posts
Comments
NSLog(); Header Image

Quick Mod_Rewrite Question

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.

11 Responses to "Quick Mod_Rewrite Question"

  1. 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.

  2. 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.

  3. 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.

  4. 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)

  5. 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.

  6. 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.

  7. 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.

  8. 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?

  9. 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

  10. Stuart got it! Thanks Stuart. I appreciate it! (Yes, Justin, the .htaccess file is at the site's root.)

  11. You could do this with RedirectMatch, no need for a rewrite rule:

    RedirectMatch /archives/international/(.*) http://blah.com/archives/other/$1