• Resolved lukemv

    (@lukemv)


    I have created a page in my root directory that pulls information from a separate mySQL database. The page works perfectly when I use the $_GET call from the URL (ie: page.php?id=123 will pull the mySQL query with id “123”).

    The URL is sloppy. I tried to use my previous code in the .htaccess:

    rewriterule ^([^-]+)-([^&]+)\.html$ /page.php?id=$1&id=$2 [L]
    rewritecond %{http_host} ^myURL.com [nc]
    rewriterule ^(.*)$ http://www.myURL.com/$1 [r=301,nc]

    When using this on a non-wordpress site, it works fine (myURL.com/Entry_Name-123.html) but with WordPress, it is breaking the WordPress pages.

    I want to keep this page.php in the root directory (outside of a wordpress theme) to avoid other problems.

    Thoughts?

Viewing 5 replies - 1 through 5 (of 5 total)
  • See here for adding non-wordpress rules into WordPress.

    http://codex.wordpress.org/Function_Reference/WP_Rewrite#Non-Wordpress_rewrite_rules

    Although that page in general should give you some ideas of how to add custom rules.

    Thread Starter lukemv

    (@lukemv)

    I don’t know if that is what you meant to send.

    i want to create a link:

    myURL.com/Name-123.html

    That, in reality, is: myURL.com/page.php?id=123

    My non-WordPress site used the code from my first post… but now with WordPress using the rewrite, i am not able to have both WordPress’s standard .htaccess with my above code. Does this make sense?

    I personally find it easier in the htaccess myself, this should work for you.. (to an extent, see below)

    <IfModule mod_rewrite.c>
    RewriteRule ^Name-123.html /index.php?page_id=18 [L]
    </IfModule>

    It should be placed before the WordPress rules. However do note, you may find WordPress performs a redirect to the page whose ID is in the above rule when trying to access the URL..

    In order to work around the redirect(assuming you don’t want it redirecting) i think you’d need to use the approach outlined on the link i posted in my last reply (i can’t say for sure, i’m honestly not familiar with the code that manages and performs the redirects for WordPress).

    EDIT: Sorry just realised you’re not using index.php(eg. passing variables to WordPress), so assuming this is a custom page.php file, just update the index.php to page.php, and perhaps WordPress won’t redirect (i don’t have your files, so i don’t know if it will).

    EDIT: Updated version of example code.

    <IfModule mod_rewrite.c>
    RewriteRule ^Name-123.html /page.php?id=123 [L]
    </IfModule>

    Or if it needs to have variable functionality(pattern matching), maybe something like.

    <IfModule mod_rewrite.c>
    RewriteRule ^([a-z]+)-([0-9]{3}).html /page.php?id=$2 [L]
    </IfModule>

    Break down of above:
    Match at least one character that is a-z, followed by a hyphen, then exactly 3 numeric values, followed by a html extesion( .html ).

    Sorry really should of re-read the opening post again before being so eager to write code… (lol, guilty of jumping the gun).

    Thread Starter lukemv

    (@lukemv)

    Perfect, thank you VERY much for your assistance. The final code that I am using is:

    <IfModule mod_rewrite.c>
    RewriteRule ^([^-]+)-([^&]+).html /PageName.php?id=$1&id=$2 [L]
    </IfModule>

    Where $1 is a name and $2 is an ID number (any size)

    So i take you’re expecting characters other then letters and numbers then. If you’re not expecting other characters, the more specific the rules are the easier and quicker it will be for the rules to run their course.

    You’re passing ID twice in that rule, you do realise only the second will be read on the receiving end right? Incase you wasn’t aware / didn’t realise.

    If the name is only likely to be made up of letters the rule would be better suited checking for that specifically, and the same with the numeric values..

    I’m not sure how much you know about pattern matching / regex, but i’ll explain them incase..
    ([^-]+) – Anything not a hypen at least once.
    ([^&]+) – Anything not an ampersand at least once.

    If you expect particular values in those areas, then you should ideally be checking for them, and not passing along other characters.

    The second rule for example, if only numeric, whether it’s 1, 10 or 100, could be..
    ([0-9]+) – A number 0-9, one or more times.

    So it would accept 5, 27, 10000, 732, 9991 , or any numeric value..

    In any case, happy to help..

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Permalinks … without messing up wordpress links’ is closed to new replies.