Justin
Justin

Reputation: 2530

301 URL Forwarding with HTACCESS or PHP

Just curious if anyone can help me on this HTACCESS issue.

I have these OLD URLS that need to get forwarded properly.

Previous structure

domain.com/Canada/Accounting

domain.com/Canada/Trades

domain.com/Canada/Sales

Proper structure

CATEGORY - /jobs/accounting-jobs

LOCATION - /jobs/jobs-kelowna

TOGETHER - /jobs/accounting-jobs-kelowna

Domain Structure

domain.com/jobs/[category]-jobs-[location]

Is this possible, either by HTACCES or PHP...just don't want these 404'ed pages. I have 86+ to do, if there is a good way to forward these. This is what I have, but i'm unable to successfully forward the bad-urls properly.

OLD

/browse
/Toronto/
/Canada/Administrative
/Vancouver/
/Canada/Trades
/Calgary/
/Canada/Hospitality

This is my HTACCESS right now.

Options +FollowSymlinks

    RewriteEngine on
    RewriteBase /

    #
    # Trailing slash check
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule ^(.*)$ $1/ [L,R=301]


    #
    # PAGES
    RewriteRule ^add-job/?$     /add-job.php [L]
    RewriteRule ^jobs/?$        /results.php [L]
    RewriteRule ^sitemap/?$     /sitemap.php [L]

    #
    # SEARCH
    # CATEGORY - accounting-jobs
    # LOCATION -            jobs-kelowna
    # TOGETHER - accounting-jobs-kelowna
    RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)?$    results.php?whatwhere=$1&page=$2
    RewriteRule ^jobs/([a-zA-Z0-9_-]+)/([0-9]+)/?$   results.php?whatwhere=$1&page=$2

Upvotes: 0

Views: 97

Answers (2)

jeroen
jeroen

Reputation: 91734

To 301 redirect your pages you can do something like:

RewriteEngine On
RewriteBase /

RewriteRule ^(\w+)/(\w+)$ /jobs/$2-jobs-$1 [R=301,L]

This only addresses the urls from your previous structure (the combinations, you have not shown any previous urls with just location or category) but note that Canada will stay Canada, it does not become canada. You can change everything to lower case using rewrite as well.

You also have to take care that you don't rewrite any of the current urls but without more information, this should do it.

Edit: For the location-only urls you could use a rule like:

RewriteRule ^(\w+)/$ /jobs/jobs-$1 [R=301,L]

Again, you need to look out that your rewrite rule does not interfere with your current urls. If that is the case, you would need to redirect every old url manually.

For lower-case new urls, you should search SO, there are some questions with good answers about converting a mized-case variable to lower-case.

Upvotes: 1

Cal
Cal

Reputation: 7157

If you have mod_rewrite, you can add these lines to your .htaccess file:

RewriteEngine on
RewriteRule ^Canada/Accounting$ /jobs/accounting-jobs [R,L]

However, it's not clear from your question exactly what you want mapped. Are the 3 previous URLs supposed to redirect to the 3 new ones? They don't seem to be equivalent.

Upvotes: 1

Related Questions