Forum Moderators: phranque

Message Too Old, No Replies

Need help with .htaccess and WordPress

.htaccess and WordPress

         

peterbra

12:28 pm on Jan 29, 2008 (gmt 0)

10+ Year Member



I want to transfer existing static site to WP.

What I need is when user / SE goes to:
http://www.example.com/filename.html

To be redirected to:
http://www.example.com/filename/

...so I need to "strip" extension and add slash instead of it :)

I tried with following (but this isn't working):

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /[^.]+\.html\ HTTP
RewriteRule ^([^.]+)\.html$ $1/ [R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule . /wordpress/index.php [L]
RewriteRule ^/$ /e/www/ [R]
</IfModule>
# END WordPress

...anyone knows why?

[edited by: encyclo at 1:29 pm (utc) on Jan. 31, 2008]
[edit reason] switched to example.com [/edit]

jdMorgan

2:35 pm on Jan 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the last rule intended to do? That rule will probably never execute with the WP rules in place. If this code is in .htaccess, then it only applies to a URL-path requested with two slashes, that is, "example.com//"

Jim

peterbra

12:01 pm on Jan 31, 2008 (gmt 0)

10+ Year Member



Actually they are in .htaccess file,
and you are right - this rule is never executed:

RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^GET\ /[^.]+\.html\ HTTP
RewriteRule ^([^.]+)\.html$ $1/ [R=301]

The second rule - that's WordPress's rule (when I installed WP it was added automatically)

All I am trying to achieve is when address is typed like e.g.
http://www.example.com/filename.html
to be re-written as:
http://www.example.com/filename/

but after that to continue WordPress rule...

Any ideas how to achieve this?

BTW: GREAT FORUM MAN - I FOUND A LOT OF HELPFUL STUFF HERE!

[edited by: encyclo at 1:30 pm (utc) on Jan. 31, 2008]
[edit reason] switched to example.com [/edit]

jdMorgan

5:01 am on Feb 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is this for? It will never execute with the code as-written:

RewriteRule ^/$ /e/www/ [R]

I think you may be confusing the function of mod_rewrite as well. Mod_rewrite does not change URLs published on Web pages, it only translates those URLs, when requested by a client (for example by clicking on a link on your page) into a different internal server path, or it generates a redirect response to the client, telling it to ask again for what it requested, but this time using a new URL.

You appear to have coded the latter function, and if WP doesn't know what to do with the .html-less, slashed URL, then you haven't accomplished anything.

If you phrase your goal in terms of "when someone clicks a link that says 'this', I want 'that' to happen" then this will be easier to discuss -- Start with a link on one of your pages and work forward from there, rather than trying to work from the middle out both ways... A click on a link is where things start.

Jim

peterbra

8:33 am on Feb 1, 2008 (gmt 0)

10+ Year Member



I want for URL to be rewritten:

In browser address bar someone type:
[webmasterworld.com...]
Can that be "rewritten" as:
[webmasterworld.com...]

...stripped extension and added slash at the end :)

jdMorgan

6:30 pm on Feb 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That sounds backwards. Here is the usual procedure.

  • Change your on-page links from /somefile.html to /somefile.
  • The search engines and browsers will then request "/somefile" from your server.
  • In mod_rewrite, detect requests for "/somefile", and internally rewrite that to the real file, "/somefile.html"
  • As an optional third step, redirect direct client requests (and only direct client requests) from "/somefile.html" to "/somefile" to correct any old incoming links and bookmarks.

    None of the above will work until you edit your pages to publish the new "/somefile" links... The links on your pages define the URL that will be used by the world.

    Jim

  • djmick200

    9:53 pm on Feb 3, 2008 (gmt 0)

    10+ Year Member



    I want for URL to be rewritten:

    In browser address bar someone type:
    [webmasterworld.com...]
    Can that be "rewritten" as:
    [webmasterworld.com...]

    ...stripped extension and added slash at the end :)

    jdMorgan

    That sounds backwards.

    jd you always comes across as a clever fellow so i was surprised when you said this sounds backwards. In what way is it backwards?

    I actually came across this thread as I was looking to do the same thing for a blog. I thought the guys question in his first post was pretty clear.

    At a guess he has moved from blogger to a wordpress setup and he wants all the url's from blogger which have .html extentions to hit the right pages in wordpress which end with a /

    jdMorgan

    10:46 pm on Feb 3, 2008 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    The code posted above *should* do what is desired, so that is where the questions come from.

    It is critically important to have a correct requirements specification before coding. My statements above were intended to elicit a response from the OP, so that the requirements can be ascertained; The code looks correct, therefore I question the requirements.

    Jim

    djmick200

    11:02 pm on Feb 3, 2008 (gmt 0)

    10+ Year Member




    The code posted above *should* do what is desired, so that is where the questions come from.

    Now I understand that, when I re-read what you posted it all makes sense. Please accept my apologies jd.

    In my search to achieve blah-blah.html becoming blah-blah/ I came across the following and found it achieved what I was looking to do.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^([0-9]{4})/([0-9]{1,2})/([^/]+)\.html$ $1/$2/$3/ [QSA,R=301,L]
    </IfModule>

    Looks a lot more than what 'should' have worked in the OP, granted I never tried it as I misunderstood and thought it wouldn't work.