0

I have two websites, elijahstreams.com (OLDSITE) and projectjustcause.com (NEWSITE).

I need ID-based URLS to forward from OLDSITE to SEO-friendly URLS on NEWSITE via .htaccess directives.

This is what my .htaccess file on OLDSITE currently looks like:

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^elijahstreams.com$ [OR]
 RewriteCond %{HTTP_HOST} ^www.elijahstreams.com$
 RewriteRule (.*)$ http://projectjustcause.com/?=$1 [R=302,L]
</IfModule>   

Redirect 302 ^/watch/watch.php?id=1314$ https://projectjustcause.com/videos/special-broadcast-with-steve-and-derene-shultz-the-story-behind-the-story/

#AddType text/html .html
#AddHandler server-parsed .html
AddHandler x-mapp-php4 .html .php .php4

#HTTPS FORWARD ADDED 04/02/19 CM 
RewriteCond %{HTTPS} !=on 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

The top lines in the are working in that whenever I type "elijahstreams.com" into a browser, the site forwards to and lands on projectjustcause.com. However, the url parameters aren't being passed. I'm trying to do a 301 redirect ultimately, but some blogs suggested best practice was to start with a 302 until you had it figured out, then switch to 301.

My 302 redirect is not forwarding to the page specified.

DESIRED RESULTS:

  1. Type https://elijahstreams.com/watch/watch.php?ID=1314 in browser address bar
  2. Browser loads page at https://projectjustcause.com/videos/special-broadcast-with-steve-and-derene-shultz-the-story-behind-the-story/
  3. The browser also should tack on the ID parameter as a folder like this:
[long-dest-url]/1314

Happy to clarify if needed. Any help greatly appreciated.

2
  • 1
    Do the old and new domains point to the same place? Can you clarify what the old and new URLs look like and what you think the "top lines" are doing? Those "top lines" don't look correct as they are converting any URL-path to an arguably malformed query string, eg. olddomain.com/foo would be redirected to newdomain.com/?=foo - is that the intention?
    – MrWhite
    Jul 13, 2023 at 15:59
  • It seems, that one of the key phrases for answering is url parameters aren't being passed
    – Tom Newton
    Jul 14, 2023 at 13:17

1 Answer 1

2

On the first set.

<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{HTTP_HOST} ^elijahstreams.com$ [OR]
 RewriteCond %{HTTP_HOST} ^www.elijahstreams.com$
 RewriteRule (.*)$ http://projectjustcause.com/?=$1 [R=302,L]
</IfModule> 

I'm confused is this a temporary "302" redirect all of the traffic (the entire) site, to the HTTP of the new site? Do you plan to put the content back on old site and want it to remain in search engines and not apply the incoming links to the new site? If that is not the case you need a 301 not a 302.

As written this rule will send a redirect to the browser so no rules under this rule will be used.

This rule can be simplified to:

 RewriteEngine On
 RewriteCond %{HTTP_HOST} elijahstreams.com
 RewriteRule (.*)$ https://projectjustcause.com/$1 [R=302]

The "^" symbol represents the start of the line for the match, in this case you want both the www and non-www forwarded. There is no need for the "^" unless you are needing to specifically only forward a particular subdomain. IE ^video.example.com$ only matches the subdomain video. In this case it seems you want the entire site.

Or you are trying to forward www.example.com to example.com and of course not forward example.com to example.com. But in this case we are going to new-example.com

The:

<IfModule mod_rewrite.c>
</IfModule>

prevents a 500 error if rewrites are used without the mod_rewrite ... if you don't have the mod_rewrite you would still get the error message because you have other rewrites in your .htaccess without these.

Personally, I prefer the error message so I know it's not working. So I generally do not take advantage of this conditional execution until I'm done with it.

The Second Rule

This line is using a query string ... and it is shadowed by the first one that will redirect the browser, IE it never sees the light of day.

Redirect 302 ^/watch/watch.php?id=1314$ https://projectjustcause.com/videos/special-broadcast-with-steve-and-derene-shultz-the-story-behind-the-story/

... and at the point in time when Apache processes the .htaccess the %{HTTP_HOST}, %{REQUEST_URI}, and %{QUERY_STRING} are separate values. In other words, you can not use Redirect with the %{QUERY_STRING} as it only uses %{REQUEST_URI} ... https://httpd.apache.org/docs/trunk/rewrite/remapping.html

You can, however,

RewriteCond %{QUERY_STRING} ^id=1314$
RewriteRule https://projectjustcause.com/videos/special-broadcast-with-steve-and-derene-shultz-the-story-behind-the-story/ [R=301,L]

This will match any request where it has a Query_String of "id=1314" at the "^" start of the query string and there is nothing else "$" after it. You could remove the "^" and "$" if you want to redirect anything that contains "id=1314" ... IE "watch.php?id=1314&other=value"

The last rule should be on the new site

#HTTPS FORWARD ADDED 04/02/19 CM 
RewriteCond %{HTTPS} !=on 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

or

<IfModule mod_rewrite.c> 
  RewriteCond %{HTTPS} !=on 
  RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
</IfModule>

Combined

Note I'm using 301 here, if they are temporary use 302

    <IfModule mod_rewrite.c>
      RewriteEngine On
      # this needs to be first.
      RewriteCond %{QUERY_STRING} ^id=1314$
      RewriteRule https://projectjustcause.com/videos/special-broadcast-with-steve-and-derene-shultz-the-story-behind-the-story/ [R=301,L]


      RewriteCond %{HTTP_HOST} elijahstreams.com
#                 contains the name of old site. 
      RewriteRule (.*)$ https://projectjustcause.com/$1 [R=301,L]

#  note this last rule is shadowed but if everything falls through IE
#  .htacess is the same on both oldsite and newsite and a request is made
#  to http://projectjustcause.com which does not match any above rule
#  it 301s to https

      RewriteCond %{HTTPS} !=on 
      RewriteRule ^(.*)$ https://projectjustcause.com/%{REQUEST_URI} [L,R=301,NE]
    </IfModule> 

#AddType text/html .html
#AddHandler server-parsed .html
AddHandler x-mapp-php4 .html .php .php4

Suggestion ...

I would recommend putting the

      RewriteEngine On
      # this needs to be first.
      RewriteCond %{QUERY_STRING} ^id=1314$
      RewriteRule https://projectjustcause.com/videos/special-broadcast-with-steve-and-derene-shultz-the-story-behind-the-story/

into /watch/.htaccess instead of /.htaccess It will work well from there and will be used before /.htaccess is used automatically. I'm assuming that other videos may be redirected as well and having a separate .htaccess to handle the video redirects would not interfere with any other portion of the site should a typo happen.

1
  • For testing redirects "make certain you have the browser cache/memory turned off" ... softkube.com/blog/… ... otherwise the browser will not be seeing what is live on the host. Jul 16, 2023 at 1:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.