Jump to content

301 Redirects in htaccess?


tires
 Share

Recommended Posts

Hi!

I just redesigned a website and switched from another cms to processwire.

Now i want to redirect the old urls to the new pages ones using 301 redirects and htaccess.

I read a few posts an tried a few things but it doesn't work.

My htccess file looks like that:

... 

# If any conditions above match, isssue a 403 forbidden
  RewriteRule ^.*$ - [F,L]

RewriteRule ^index.php?id=33$ /products/ [R=301,L]
RewriteRule ^index.php?id=36$ /contact/ [R=301,L]
RewriteRule ^index.php?id=37$ /imprint/ [R=301,L]

  # -----------------------------------------------------------------------------------------------
  # Ensure that the URL follows the name-format specification required by ProcessWire
  # -----------------------------------------------------------------------------------------------

  RewriteCond %{REQUEST_URI} "^/~?[-_.a-zA-Z0-9/]*$"

...

For example: The old product page url was "index.php?id=33" and i want to redirect this to  the new "/products" page.

But that doesn't work?

Can you tell me where is my error?

Thanks and regards!

Link to comment
Share on other sites

I think it is much easier to manage this kind of redirects with modules rather than manually in htaccess. Check those out:

http://modules.processwire.com/modules/process-jumplinks/

http://modules.processwire.com/modules/process-redirects/

Sorry for not answering your question directly, but I am really sure it is a better way (as you do not have to care about manually upgrading htaccess while upgrading Processwire).

Edited by Ivan Gretsky
  • Like 2
Link to comment
Share on other sites

not totally sure (untested) but since /index.php loads the homepage,  you might be able to allow get vars on the homepage template and then put in some logic there to process the request...

if($input->get->id) {
    
    $pid =  (int) $input->get->id;
   
    if($pid === 33) {
        $fwd = $pages->get('/products/');
        $session->redirect($fwd);
    }

    if($pid === 36) {
        $fwd = $pages->get('/contact/');
        $session->redirect($fwd);
    }

    if($pid === 37) {
        $fwd = $pages->get('/imprint/');
        $session->redirect($fwd);
    }
    
}
  • Like 1
Link to comment
Share on other sites

Thanks for your answers!!!

I tried both modules (redirects and jumplinks) but they don't work for me.

And for SEO reasons i would like to have 301 redirects.

So i think the best place for the redirects is the htaccess file.

How do you handle this?

Link to comment
Share on other sites

I am no expert in htaccess commands so won't help you out with this. But I can't see why those redirect modules do not fit. They return 301 http status codes as far as I remember - you can check yourself with something like this. I still vote for going "the module way" here.

  • Like 2
Link to comment
Share on other sites

Thanks for your answers!!!

I tried both modules (redirects and jumplinks) but they don't work for me.

And for SEO reasons i would like to have 301 redirects.

So i think the best place for the redirects is the htaccess file.

How do you handle this?

Hi :-)

If you don't mind, please would you let us know why Jumplinks doesn't work for you? Redirects isn't able to handle index.php?something requests, but Jumplinks is. In your case, setting it up would be as simple as this:

Step 1: Create a new Jumplink. Source: index.php?id={id} Destination: {id|pages}/

Step 2: Create a Mapping Collection called pages, containing the following Collection Data:

33=products
36=contact
37=imprint

I'm also sure you'd have more of those to add.

Jumplinks will then check the incoming request, find a match, map the page ID to the respective page in the collection, and redirect with a 301 status code accordingly.

Really easy :-)

  • Like 3
Link to comment
Share on other sites

Thank you Mike for your detailed answer!!!

I tried your proposed configuration but without success.

Here is what i entered:

Jumplink

Source: index.php?id={id}

URI/URL: {id|pages} outputs an "destinationUriUrl" error

than i entered: http://www.mydomain.de/mypath/to/processwire/{id|pages}

Mapping Collections

(collection name "pages")

Mapping Data:

33=products
36=contact
37=imprint

...

Can you find the error?

???

Thanks!!!

Link to comment
Share on other sites

Hi!

The version is 2.5.3.

The displayed error is (translated form german): "destinationUriUrl: Error found - please check if the url is valid."

The error is only displayed if i open the jumplink settings once again. There was no error displayed when is saved it ...

Here are the screenshots:

post-2761-0-80241200-1429269420_thumb.jp

post-2761-0-21272800-1429269418_thumb.jp

Link to comment
Share on other sites

I have not used this module but judging from the screenshot you should enter the URI/URL without a leading slash, and you seem to have done it with a leading slash.

Try entering:

{id|pages}/

and see if it works.

  • Like 1
Link to comment
Share on other sites

Indeed, though I am surprised it was not trimmed automatically. The following line of ProcessJumplinks.module is supposed to automatically trim it:

$destination = ltrim($this->db->escape_string($this->sanitizer->url($input->destinationUriUrl)), '/');

If removing the leading slash does not work, please let me know.

Link to comment
Share on other sites

Thanks for your answers!!!

Of course i entered the url without a leading slash!!!

{id|pages}/

In the overview it is displayed correct.

If i open the settings once again, the error is displayed an there is the leading slash!

Should i try to change the code in the module anyway?

Here is a screenshot of the overview page:

post-2761-0-75605500-1429277011_thumb.jp

Link to comment
Share on other sites

Unfortunately, I am not able to reproduce that on my side. Is anyone else able to?

Edit: My Apologies - I didn't notice the version you're running. I can confirm this on 2.5.3. Quite weird, because I don't recall this happening before. Will look into it shortly.

Fixed: I have submitted a fix in the dev branch. The error is due to the fact that the field is an InputfieldURL, and the validation for that type of field has changed since 2.5.3. So, I have changed it to InputfieldText for now. A few months after PW 2.6 is released, I'm going to make this module compatible only with that version. However, I will leave a legacy version for those who, for whatever reason, need to stay on 2.5.3.

  • Like 2
Link to comment
Share on other sites

Thanks a lot for your fix!!!

In the meantime i possibly found where the problem is.

My installation is not in the root but in the www.mydomain.de/the/path/to/my/dev

I think i should have to put this path in the source an destination (/the/path/to/my/dev/{id|pages}/) field. Is that right?

When the site is in the root unter www.mydomain.de everything works as i should!!!

Link to comment
Share on other sites

Thanks a lot for your fix!!!

In the meantime i possibly found where the problem is.

My installation is not in the root but in the www.mydomain.de/the/path/to/my/dev

I think i should have to put this path in the source an destination (/the/path/to/my/dev/{id|pages}/) field. Is that right?

When the site is in the root unter www.mydomain.de everything works as i should!!!

I may have missed something somewhere there, but Jumplinks does work when you're in a sub-directory (my test-cases worked), and so your destination should only contain the path after your installation root. Nonetheless, I will look into it.

Link to comment
Share on other sites

Ok. Perhaps that info should be mentioned somewhere ...

Now that i know these issue i can either use a subdomain for developing or do the redirects later on the live server. So no problem!

Thanks for your help and the great module!!!

  • Like 1
Link to comment
Share on other sites

Oh no ... there is another redirect issue i have to solve. ???

There are two domains that should point on the same site.

One of them should redirect with 301 to the main domain.

I tried so add this code in the htaccess-file but without success:

    RewriteCond %{HTTP_HOST} !^http://www.myseconddomain\.de$ [NC]
    RewriteRule ^(.*)$ http://www.mymaindomain.de [R=301,L]

i added this lines here:

... 
# -----------------------------------------------------------------------------------------------
  # OPTIONAL: Redirect users to the 'www.' version of the site (uncomment to enable).
  # For example: http://processwire.com/ would be redirected to http://www.processwire.com/
  # -----------------------------------------------------------------------------------------------

   RewriteCond %{HTTP_HOST} !^www\. [NC]
   RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

    RewriteCond %{HTTP_HOST} !^http://www.myseconddomain\.de$ [NC]
    RewriteRule ^(.*)$ http://www.mymaindomain.de [R=301,L]

  # -----------------------------------------------------------------------------------------------
  # OPTIONAL: Send URLs with non name-format characters to 404 page
  # Remove this section if it interferes with URLs of any other software you may be running.
  # -----------------------------------------------------------------------------------------------
...

Link to comment
Share on other sites

Your code is saying the following: If the domain is not the second domain, then redirect to the main domain. As such, you should change it to this:

Option 1: "If the domain is not the main one, redirect to the main one"

RewriteCond %{HTTP_HOST} !www\.mymaindomain\.de$ [NC]
RewriteRule ^(.*)$ http://www.mymaindomain.de/$1 [L,R=301] 

Option 2: "If the domain is the second one, redirect to the main one"

RewriteCond %{HTTP_HOST} ^www\.myseconddomain\.de$ [NC,OR]
RewriteRule ^(.*)$ http://www.mymaindomain.de/$1 [L,R=301] 
Edited by Mike Anthony
Link to comment
Share on other sites

YEAH!!!

Thanks a lot. Now everythings works well!

BTW. The second option didn't work. Perhaps because the lines above still redirect the non-www to the www url ...

Thanks a lot and a nice day!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...