Link to home
Start Free TrialLog in
Avatar of Brad N
Brad N

asked on

.htaccess to new site AND change a character in the URL

I am wanting to change to a new website.
Struggling to get working htaccess code to do the job

OLD WEBSITE
http or https://www.oldwebsite.com.au/directory/this_is_page_1.php

where directory may change to another word
and the page name may change.
and there may be up to 6 hyphens that need to become dashes in the file name

NEW WEBSITE
https://www.newwebsite.edu.au/directory/this-is-page-1.php
(Same file structure, but hyphens become dashes)
Avatar of David Favor
David Favor
Flag of United States of America image

Likely best way to do this...

1) Rename your newsite directory to include dashes.

2) Create entries in your .htaccess on old site, new site or both, redirecting the underscore links like this_is_page_1.php to the this-is-page-1.php links which include dashes.

3) Do not ever use 301s for your redirects. Use 302s, because any typo in a 301 is cached for all visitors in their browsers forever, so you can never change a 301 + effect any previous visitor, you must keep layering on additional 301s.

Better to use 302s, so changes you make effect all previous + new visitors.

I've seen many a small fortune lost by people using 301s.

4) If you require more help, post your actual URLs (old + new site). Also attach a copy of your full .htaccess file.
Aside: If you have 1000s+ URLs to rewrite, developing a .php file to handle all 404s, transforming underscores into dashes will be far easier to debug + manage, than creating 1000s+ RewriteRule entries in your .htaccess file or using complex RewriteRule regular expressions.
.htaccess is not the ideal place to do this type of "replacement", however the below should accommodate the scenario you are describing. Note that this is based on there being a folder name (containing letters and numbers only) in front of the final page, as mentioned in your question. If you have other entries in your .htaccess then these new rewrite rules should be placed at the start of your .htaccess

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteRule !\.(html|php)$ - [S=6]
RewriteRule ^([a-zA-Z_]+)/([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1/$2-$3-$4-$5-$6-$7-$8 [E=underscores:Yes]
RewriteRule ^([a-zA-Z_]+)/([^_]*)_([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1/$2-$3-$4-$5-$6-$7  [E=underscores:Yes]
RewriteRule ^([a-zA-Z_]+)/([^_]*)_([^_]*)_([^_]*)_([^_]*)_(.*)$ $1/$2-$3-$4-$5-$6 [E=underscores:Yes]
RewriteRule ^([a-zA-Z_]+)/([^_]*)_([^_]*)_([^_]*)_(.*)$ $1/$2-$3-$4-$5 [E=underscores:Yes]
RewriteRule ^([a-zA-Z_]+)/([^_]*)_([^_]*)_(.*)$ $1/$2-$3-$4 [E=underscores:Yes]
RewriteRule ^([a-zA-Z_]+)/([^_]*)_(.*)$ $1/$2-$3 [E=underscores:Yes]

RewriteCond %{ENV:underscores} ^Yes$
RewriteRule (.*) https://www.newwebsite.edu.au/$1 [R=301,L]

Open in new window

Whether you use a 301 (permanent) or 302 (temporary) redirect is your decision, however keep in mind that if you use 302 then any backlinks across the web, and search engines in particular, will not update their link. With a 301 then most search engines will update link accordingly.
Avatar of Brad N
Brad N

ASKER

Thank you both for your assistance.

OLD
https://www.easyhr.com.au/contact/contact_quote_request.php
NEW
https://www.easyhr.edu.au/contact/contact-quote-request.php

OLD
https://www.easyhr.com.au/courses/diploma_hr_bsb50618.php

NEW
https://www.easyhr.edu.au/courses/diploma-hr-bsb50618.php


Most file names are the same, but there are a few that I will deal with on a case by case basis.
I realise these would need to be managed above 'standard conversion' scripts.

All up I think there would be about 200 pages.

David - you suggest a php script similar to

1 - get page url
2 - Str replace _ with
3 - forward user to new page

I would need this on every 'old page' to trigger a redirect to a new page.
a standard 404 won't work as some pages don't follow the above rules.

Why is this  a better solution than htaccess?

Thanks for your assistance
Best avoid 301s, especially in your case (well in all cases really)... because...

301s cache forever in browsers.

If you make any slight mistake, any visitor will have that mistake cached forever.

Also, if a year from now, you'd like to change the target of a 301, you can't... well... you can, just no previous visitor will ever see your change.

Be aware, using a 301 is like painting yourself into a corner. There's no way out, short of a big mess.
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.