SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    rewrite rule for specified pages in wordpress, and add .htm extension

    Hey guys,

    New here....and to all this with mod_rewrite and .htaccess stuff!

    But anyway, I was hoping for a friendly soul that would give me a kick in the right direction.

    I recently redesign a page and used wordpress for CMS and have permalinks activated. So it looks like 'mydomain.com/mypage'

    The same page on the old site had the same name but with a .htm extension. And that page had a PageRank of 3, so if I check 'mydomain.com/mypage.htm' it still has PR but 'mydomain.com/mypage' has not...

    So, i want 'mydomain.com/mypage' to be 'mydomain.com/mypage.htm' and the same goes for at lest three other pages that had PR.

    Is this possible to by adding rewrite rules in .htaccess? But only to apply to wordpress pages NOT posts! Or can I do it "url" by "url"?

    All help would be appreciated!

  2. #2
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like:
    Code:
    RewriteRule /([a-z]+)\.html /$1
    will redirect all links like /asdf.html to /asdf.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, doesn't work, I still get the "don't exist" if I type in 'thepage.htm' and my 404 page is displayed?

    EDIT: ..and wouldn't that rewrite ALL permalinks not only the one that are 'wordpress pages'?
    Last edited by kungknas; Jun 24, 2008 at 09:33.

  4. #4
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, doublechecked that:

    Code:
    RewriteEngine On
    RewriteRule ^([a-z0-9]*)\.html $1
    works well for me:
    http://localhost/1.html is redirected to http://localhost/1
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  5. #5
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    First, thanks for taking the time!

    ...but it's not working. I don't know If I'm expecting it to do something it cant!

    Ok, this is my .htaccess

    Code:
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteRule ^([a-z0-9]*)\.htm $1
    </IfModule>
    
    # END WordPress
    And this is the page in question, mmaphuket.com/accommodation (no pagerank) before I redesign it that page was mmaphuket.com/accommodation.htm and that page still got pagerank 3

    ....so, I want them "to be the same" before Google does the next PageRank update!

  6. #6
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    # this redirects any request that ends with .htm and 
    # tells that the resource is moved permanently so the bot updates the links.
    RewriteCond &#37;{REQUEST_URI} ^.*\.htm$
    RewriteRule ^([a-z0-9]*)\.htm $1 [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    RewriteRule ^([a-z0-9]*)\.htm $1
    </IfModule>
    
    # END WordPress
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  7. #7
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It works! Thanks!

    ...and does that means that the PR will be "transferred"?

  8. #8
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm not sure, but I've used this technique on several sites and it seems that PR is transferred for pages that are handled with 301 redirect.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  9. #9
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah, ok, I see....that's not really a rewrite rule, it's a redirect!? Right?

    So I can use that instead only for the old pages with PR and redirect them to the new ones? Because I have some new pages that has a slightly different name exept the .htm extension and want to avoid to rename all the internal links...

    So, if I want to redirekt mydomain.com/contact.htm to mydomain.com/contact_us how would I write, and can I just duplicate that line for the rest of the pages?

  10. #10
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > that's not really a rewrite rule, it's a redirect!? Right?
    This is rewrite rule that cause a redirect.

    > So I can use that instead only for the old pages with PR and redirect them to the new ones?
    Yes.

    > So, if I want to redirekt mydomain.com/contact.htm to mydomain.com/contact_us how would I write, and can I just duplicate that line for the rest of the pages?

    Here is part of my .htacces for some site, long history, several CMS changed:
    Code:
      RewriteCond &#37;{REQUEST_URI} =/blog/2/atom					[OR]
      RewriteCond %{REQUEST_URI} =/blog/view/feed/id/2				[OR]
      RewriteCond %{REQUEST_URI} =/export.php					[OR]
      RewriteCond %{REQUEST_URI} =/Blog/Atom/					[OR]
      RewriteCond %{REQUEST_URI} =/Blog/Atom
      RewriteRule ^.*$ /rss.xml [L,R=301] 
    
      RewriteCond %{REQUEST_URI} =/articles/view/list/id/93				[OR]
      RewriteCond %{REQUEST_URI} =/articles/view/list/id/93/			[OR]
      RewriteCond %{REQUEST_URI} =/articles/view/list/id/93/categories/orm
      RewriteRule ^.*$ /node/6 [L,R=301]
    and about 20 similar blocks.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  11. #11
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, am I on the right track here? Would this .htacces


    Code:
    # BEGIN WordPress
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{REQUEST_URI} ^.*\.htm$
    RewriteRule ^([a-z0-9]*)\.htm $1 [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/contact.htm
    RewriteRule ^.*$ /contact_us [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/facilities.htm
    RewriteRule ^.*$ /the_facility [L,R=301]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    # END WordPress
    Take care of all the pages that has the same name as before?
    (for ex. mmaphuket.com/accommodation.htm -> mmaphuket.com/accommodation)

    And then the two rules for the pages that doesn't have the same name?
    (for ex. mmaphuket.com/contact.htm -> mmaphuket.com/contact_us)

  12. #12
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to move
    Code:
    RewriteCond &#37;{REQUEST_URI} =/contact.htm
    RewriteRule ^.*$ /contact_us [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/facilities.htm
    RewriteRule ^.*$ /the_facility [L,R=301]
    before

    Code:
    RewriteCond %{REQUEST_URI} ^.*\.htm$
    RewriteRule ^([a-z0-9]*)\.htm $1 [L,R=301]
    because now it will catch contact.htm and facilities.htm and convert them to
    contact and facilities, so they will be applied on already translated url.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ok, what if I only rewrite the pages with actually pagerank and that exist in both the old and the new site! Like this...

    Code:
    RewriteCond %{REQUEST_URI} =/accommodation.htm
    RewriteRule ^.*$ /accommodation [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/links.htm
    RewriteRule ^.*$ /links [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/faq.htm
    RewriteRule ^.*$ /faq [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/contact.htm
    RewriteRule ^.*$ /contact_us [L,R=301]
    
    RewriteCond %{REQUEST_URI} =/facilities.htm
    RewriteRule ^.*$ /the_facility [L,R=301]
    That would do it right?

  14. #14
    SitePoint Addict
    Join Date
    Mar 2006
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just add something like this to your htaccess file

    Code:
    Redirect 301 /accommodation.htm http://www.mmaphuket.com/accommodation
    Redirect 301 /links.htm http://www.mmaphuket.com/links
    Redirect 301 /faq.htm http://www.mmaphuket.com/faq

  15. #15
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aaronjj, thank you for the nice solution.

    kungknas, yes. And you can try what aaronjj have suggested.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  16. #16
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Question: What in the heck is
    Code:
    RewriteCond &#37;{REQUEST_URI} =/accommodation.htm
    RewriteRule ^.*$ /accommodation [L,R=301]
    ... supposed to do?

    I've NEVER seen = in a match situation like this and .* is an abuse of the server for no apparent reason.
    Code:
    RewriteRule ^/?accommodation\.htm$ accommodation [L,R=301]
    ... will do what's intended AND the optional leading slash is only there because you didn't say which flavor of Apache you're using (Apache 1.x requires the / after ^ in the DocumentRoot, Apache 2.x thinks it's already there and will complain if it sees ^/ while ^/? keeps both versions happy).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  17. #17
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    Question: What in the heck is
    Code:
    RewriteCond %{REQUEST_URI} =/accommodation.htm
    RewriteRule ^.*$ /accommodation [L,R=301]
    ... supposed to do?
    I don't know!?

    ...that's why I asked you guys!

    And thanks for your aswer!

  18. #18
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    dklynn,

    The rule:
    Code:
    RewriteRule ^/?accommodation\.htm$ accommodation [L,R=301]
    converts URL
    Code:
    http://localhost:8081/accommodation.htm
    in
    Code:
    http://localhost:8081/C:/Inetpub/wwwroot/accommodation
    Adding slash before accommodation solves that:
    Code:
    RewriteRule ^/?accommodation\.htm$ /accommodation [L,R=301]
    Is it correct?
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  19. #19
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    358
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Anyway, aaronjj's solution looks better then using RewriteRule, so you can try to use it.
    I'm creating trouble-free Apache, PHP, MySQL installer, WITSuite,
    and use it to setup my development environment.
    Demo, support, contact. Questions?

  20. #20
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    64
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So, slash or no slash!?

    ...the redirect still works, in the browser anyway!

  21. #21
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,653
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    kk,

    Using the leading slash in the redirect sometimes causes problems. It's supposed to be DocumentRoot but can occasionally make Apache go nuts 'cause it'll looking to its own root for the file (physical path vs VH path) - despite no logical reason for it to do so!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •