Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Earlier on my site i haven't used php urlencode function to encode my url query parameters and google crawled those errors. Now after implementing the change, those indexed old urls are giving 404 response code.I tried to redirect old urls to new via htaccess rewrite but it's not working.

Suppose I want to redirect /category/Science.html to new url /category/Science, Engineering & Technology.html. For Science, Engineering & Technology, i am getting 'Science%252C%2BEngineering%2B%2526%2BTechnology' via urlencode function.

When i tried to rewrite the urls via this rule:

RewriteRule ^category/Science.html$ /category/categ.php=Science%252C%2BEngineering%2B%2526%2BTechnology[R=301,L]

it doesn't work.

Please suggest the proper way to solve this issue.

share|improve this question
1  
sidenote: suggest NOT to use filename with space and special characters. use alphanumeric characters only. – Shivan Raptor Jun 6 at 6:17
Science, Engineering & Technology.html is not a file name, but 'Science, Engineering & Technology' is value of a url query parameter. I am generating pretty urls via mod rewrite. Filename is categ.php – rammurtee Jun 7 at 18:54

1 Answer

up vote 0 down vote accepted

Your file names should only include lower-case letters, numbers, hyphens, and underscores for reasons like this. If for whatever reason, you're unable to rename your files, you can do some PHP trickery:

.htaccess

ErrorDocument 404 404.php

404.php

<?php
switch($_SERVER['REQUEST_URI']){
    case "/category/Science.html":
        header('HTTP/1.1 301 Moved Permanently');
        header("Location: /category/".urlencode("Science, Engineering & Technology").".html");
        exit();
    break;
    ...
}

// Custom 404 page stuff

I'd recommend against this because it's sloppy, hard to manage, and not expected behaviour, but if you're unable to rename the files, go for it.

share|improve this answer
1  
sidenote: remember, after header('Location: $url');, issue exit() – Shivan Raptor Jun 6 at 6:36
Thank you. I forgot about that one. I'll edit my answer right now. – Mister Melancholy Jun 6 at 6:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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