.htaccesshttp-redirect

htaccess - redirect domain to a specific page, unless a path is spefcified


I have an old url that I need to redirect to a specific page on my new site -- this part is working fine. That code is below.

RewriteCond %{HTTP_HOST} ^oldurl.org$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.org$ [OR]
RewriteCond %{HTTP_HOST} ^oldurl.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.oldurl.com$ 
RewriteRule (.*)$ https://newurl.com/the/old/page/$1 [R=301,L]

The issue is that occasionally I need to use a specific path from the old site and point it at a different url on the new site.

Is there a way to redirect to a specific page when the old url has no path specified, but when there is a path, to keep it and add it to the new url?

so oldurl.com ==> newurl.com/the/old/page

but oldurl.com/anything-else ==> newurl.com/anything-else


Solution

  • Let's carry together what we concluded in the comments to the question that help to understand what exactly you want to achieve ...

    I assume you serve both domains (oldurl.com and newurl.com) from the same server (since you added RewriteCond statements). I took the liberty to simplify them a bit. If those rules get applied on a server that only responds to requests to oldurl.com and oldurl.org then you can simply omit those conditions.

    The existing RewriteRule is a bit miss leading in your question. It captures the path of the requested URL and appends it to the target URL. Which is NOT what you want, according to your comment. So I changed it.

    RewriteEngine on
    
    # redirect https://oldurl.com/ to https://new-specific-url/the/old/page/
    RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^(www\.)?oldurl.com$
    RewriteRule ^/?$ https://new-specific-url/the/old/page/ [R=301,L]
    
    # redirect https://oldurl.com/<anything> to https://new-specific-url/<anything>
    RewriteCond %{HTTP_HOST} ^(www\.)?oldurl\.org$ [OR]
    RewriteCond %{HTTP_HOST} ^(www\.)?oldurl.com$
    RewriteRule ^ https://newurl.com%{REQUEST_URI} [R=301,L]
    

    As explained in my first comment the more specific rule comes first (so further up in the file). Think of it as an exception. After that follows the general fallback rule.

    I used ^/?$ as matching pattern in the first rule (as opposed to just ^$ as C3roe suggested it in his comment). Comes out the same here, but I personally prefer to implement rules such that they can be used without changing them in the central configuration or in distributed configuration files. If in doubt you should always prefer the central configuration, if you have access to that . Otherwise, so if you are using a cheap hosting provider for example, you can use distributed configuration files, if that feature is enabled for that location , though that comes with a few disadvantages.

    An alternative to the second rule is something like RewriteRule ^/?(.+)$ https://newurl.com/$1 [R=301,L]. It explicitly captures the requested path in the matching pattern and reuses the captured sequence. But since that is already contained in the special variable %{REQUEST_URI} I personally prefer that one.

    Please take the time to actually understand the implementation. Do not blindly copy it. You want to learn.

    You can play around with above suggestion using a simple test site.