(Wordpress) Rewrite rule or permanent redirect in .htaccess

Hope this ends up in the right forum! Sorry if it didnt! :slight_smile:

Ok, so, this is my problem…

Some time ago a moved a static website to Wordpress…

…and I didnt wanna put a permanent redirect on all old urls (and wanted it to look like static webpages and have the same extension on both posts and pages) so I installed a plugin that added .html in the end of wordpress pages. (http://www.introsites.co.uk/wordpress/html-on-pages-plugin.html)

Some pages though, uses pagination when displaying posts…and on those I’ve ended up with urls that looks like this:

The “first page” www.mysite.com/news-arhive.html …but when it starts to split up in pages it becomes www.mysite.com/news-arhive.html/page/2 etc

…which is pretty ugly!

So I either need a rewrite rule that moves the .html extension to the end of the url for the pages that have pagination.

But I rather clean it all up and just deactivate the plugin and just take out the .html from all urls… (because they dont really do much do they!?)

But then I need a permanent redirect that redirects all the old urls that ends in .html to none .html which I could probably google my way to but what about the www.mysite.com/news-arhive.html/page/2 type urls?

Do I need to make a redirect for each of those urls?

Any help and pointers would be appreciated!

Thanks!

Back again! :slight_smile:

…and back to the second part of my original post. How would I do a rewrite rule to take out the .html extension that get put in the middle of the URL?

Meaning these type of ULR’s:

www.mysite.com/news-arhive.html/page/2
www.mysite.com/news-arhive.html/page/3
www.mysite.com/news-arhive.html/page/4

etc…get rewriten to this:

www.mysite.com/news-arhive/page/2.html
www.mysite.com/news-arhive/page/3.html
www.mysite.com/news-arhive/page/4.html

And at the same time NOT interfering with the existing rewrite rule that puts .html in the end of all posts and pages.

…and thanks again for all the answers already given!

:slight_smile:

Yeah, I did, but I wanna minimize the URL changes in the search engines index…

…and even though I understand that permanent redirect rule will pass on all the PR and backlinks benefits from the old URLs I’m kinda a bit nervous… :wink:

My .htacces is posted in the beginning, still looks the same with the exception that I’ve taken out the IF testing for mod_rewrite.c

And here are the few rows of code from the plugin that puts .html in the end of the permalink structure of Wordpress pages: (if it matters?)

add_action('init', 'html_page_permalink', -1);
register_activation_hook(__FILE__, 'active');
register_deactivation_hook(__FILE__, 'deactive');


function html_page_permalink() {
    global $wp_rewrite;
 if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
 }
}
add_filter('user_trailingslashit', 'no_page_slash',66,2);
function no_page_slash($string, $type){
   global $wp_rewrite;
    if ($wp_rewrite->using_permalinks() && $wp_rewrite->use_trailing_slashes==true && $type == 'page'){
        return untrailingslashit($string);
  }else{
   return $string;
  }
}

function active() {
    global $wp_rewrite;
    if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
        $wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
 }
  $wp_rewrite->flush_rules();
}    
    function deactive() {
        global $wp_rewrite;
        $wp_rewrite->page_structure = str_replace(".html","",$wp_rewrite->page_structure);
        $wp_rewrite->flush_rules();
    }

It’s not a matter of directly after, it’s a matter of anywhere after. Might be the next character, might also be 10 characters later … :slight_smile:

David,

RewriteRule ^([^.]+)\.html([^.]*) $1$2 [R=301,L]

That assumes there will never be a dot after .html. If there is, it screws everything up.

So I’m going to propose something very unorthodox: use the :kaioken: ANYTHING :kaioken: atom!

It’s only good at one thing: blindly eat up everything it sees, which is exactly what you want here

So:
RewriteRule ^([^.]+)\.html(.*) $1$2 [R=301,L]

I feel weird actually suggesting to use that dreaded thing, but it works!

kungk,

It sounds like what you’re trying to achieve should be easy (WP is good with title-based redirections) but your plugin is mucking things up. POST your current .htaccess if you expect help, though, and be prepared to remove that plug-in 'cause it’s messing with your query string (or something).

It’ll probably help to SEE some old URIs (with their query string) and what you want to see now.

Regards,

DK

kung,

I fear that ScallioXTX is correct as messing about with links created by a CMS is the same as messing with how the CMS handles those requests. In other words, if you change those links, you MUST change the modules that handle those links. With the way that all CMSs are “spaghettified,” you’re in for a tough time.

As for the “normal” insertion of a file in a directory slot in the path/to/file, you’ll need the Options +MultiViews to serve the directory (file). I keep away from that like poison as it confuses the situation with the links.

Regards,

DK

In other words you’re not using the .htaccess at all for the redirects (well, except for routing everything to index.php), but just the wordpress code?
If so, I think you’d better head over to our Wordpress forum and ask your question there :slight_smile:

Ok, the <IfModule> is already gone! :rolleyes: …thanks, had no clue about that!

And the only character that is going to appear directly after the .html is an /

Thanks again for taking the time! :slight_smile: I appreciate it!

In that case, either/or.

Thanks for not taking offence. I DO go off on things like that - justifiably so! A webmaster should NOT be using code he/she does not understand. It’s obviously not personal, it just gets my tidy whities in a twist! Anyway, I needed that reminder to create Rant #4 and was able to give it a test! :lol:

Regards,

DK

kung,

It upsets me GREATLY to see someone making tests repeatedly (as if expecting a different result EVERY @#$% time) so, yes, get rid of the <IfModule> wrappers. If you used those on my server, you’d be GONE (for wasting a valuable resource).

[rant #4][indent]The definition of an idiot is someone who repeatedly does the same thing expecting a different result. Asking Apache to confirm the existence of ANY module with an <IfModule> … </IfModule> wrapper is the same thing in the webmaster world. DON’T BE AN IDIOT! If you don’t know whether a module is enabled, run the test ONCE then REMOVE the wrapper as it is EXTREMELY wasteful of Apache’s resources (and should NEVER be allowed on a shared server).[/indent][/rant 4]

The RewriteBase directive is designed to UNDO a mod_alias Redirect. Where is it in your .htaccess?

The leading / in the redirect is also problematic as it causes Apache to look at its ROOT directory for the index.php file before going to your %{DocumentRoot}. Because your .htaccess is IN your DocumentRoot, the / is superfluous, too.

As for the regex on that redirection, take your pick between ScallioXTX and my versions - there is VERY little to differentiate them, only the dot character after .html. If there is ANY possibility of having that dot character, use ScallioXTX’s version.

Regards,

DK

Ok, so I dont need the <IfModule> …and the rewrite base you also marked red, dont need that either?

And yes, I just want o get rid of the .html extension, I’m keeping the same site structure. With maybe a few exceptions but then I was just gonna put a rewrite on each url in question.

So…

RewriteRule ^([^.]+)\.html([^.]*) $1$2 [R=301,L]

…is the way to go then?

Rémon,

Yes, it does! However, there shouldn’t be a second dot character BECAUSE MultiViews is putting the filename in a directory position in the path. The objective of the $2 is to pick-up the remaining garbage with either will do. While I prefer the SLIGHTLY more restrictive ([^.]*), the :kaioken: EVERYTHING :kaioken: atom will work for the second filename case, too (which should not be necessary - as per his example - but …).

Regards,

DK

kung,

I would advise a test server to NOT muck-up your PR while sorting this out!

I need to create a new RANT for those who insist on testing the existence of a module in .htaccess. In this case, you’re testing TWICE for every request! GET RID OF THOSE <IfModule> … </IfModule> WRAPPERS! They’re meant to protect the CLUELESS from 500 errors when the module is not there. If you are a webmaster, KNOW whether it’s there and STOP THE SENSELESS TESTING!

BEFORE redesigning your website, I would hope that you assessed what you have, what you want to preserve and where you’re going. You have ~500 existing links that you want to preserve (for PR) so you need to retain their filenames SOMEHOW. Whether that’s in a RewriteMap or a PHP script simulating a RewriteMap, it’s a matter of whether you have access to the server configuration file. You MAY be able to circumvent the RewriteMap IF (and ONLY IF) your new “file” names are identical to the old as you won’t have to do anything at all! Okay, maybe just strip the .html off the filename. THAT is why you need to do this BEFORE redesigning your website.

[COLOR="Red"]<IfModule mod_rewrite.c>[/COLOR]
RewriteRule ^old-url\\.htm$     /info/new-url.html     [R=301,L]
[COLOR="Red"]</IfModule>[/COLOR]

# BEGIN WordPress
[COLOR="Red"]<IfModule mod_rewrite.c>[/COLOR]
RewriteEngine On
[COLOR="Red"]RewriteBase /[/COLOR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . [COLOR="Red"]/[/COLOR]index.php [L]
[COLOR="Red"]</IfModule>[/COLOR]

# END WordPress

So, {What’s the ‘So’ for?} I have 3 (or 2 really) types of URLs that gonne change when I deactivate the plugin and change the permalink structure for the posts:

http://www.mysite.com/a-example-of-a-post.html
http://www.mysite.com/a-page.html
http://www.mysite.com/a-page-with.html/pagination/2

So I want a redirect rule that redirect the above type URLs to this:

http://www.mysite.com/a-example-of-a-post
http://www.mysite.com/a-page
http://www.mysite.com/a-page-with/pagination/2

[indent]Okay, strip .html is simple and … ARGH … strip embedded .html from a MultiViews link! DOUBLE ARGH! Okay, that can ALL be done with a simple substitution RewriteRule, e.g.,

RewriteRule ^([^.]+)\.html([^.]*) $1$2 [R=301,L][/indent]

Regards,

DK

Could you post the .htaccess you have so far?
And, didn’t you say earlier you wanted to get rid of .html at the end of URLs? :wink:

Ah, ok, thanks for clarifying, but no . at all in sight! :slight_smile:

Thanks for the reply!

Ok, so, I’m gonna delete the plugin and stop using the .html ending on the urls for both pages and posts…

My current .htaccess file just contains the segment that wordpress puts there for the custom permalinks and then some old redirects.



<IfModule mod_rewrite.c>

RewriteRule ^old-url\\.htm$     /info/new-url.html     [R=301,L]

</IfModule>

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

So, I have 3 (or 2 really) types of URLs that gonne change when I deactivate the plugin and change the permalink structure for the posts:

http://www.mysite.com/a-example-of-a-post.html
http://www.mysite.com/a-page.html
http://www.mysite.com/a-page-with.html/pagination/2

So I want a redirect rule that redirect the above type URLs to this:

http://www.mysite.com/a-example-of-a-post
http://www.mysite.com/a-page
http://www.mysite.com/a-page-with/pagination/2

Got about 500 indexed pages and many inner pages with PR2+ and really dont wanna lose any of those or get any other trouble with the indexed urls.