Apache URL Re-writing not functioning properly

前端 未结 12 1474
春和景丽
春和景丽 2021-01-17 21:31

I am trying to use apache-rewrite rule to convert the below URL:

http://localhost/foo/bar/news.php?id=24

Into this

相关标签:
12条回答
  • 2021-01-17 22:17

    Looks like you've forgotten the slash in front of “news.php”.

    0 讨论(0)
  • 2021-01-17 22:18

    The substitution (Real) URL has a number -Code- to identify the link (According to your description): http://localhost/DIRECTORY/AID/news.php?news=42

    That code is 42 in this case, but the URL you want displayed doesn't have it. Without that number, we'll get error 404 always. It's like entering only: http://localhost/DIRECTORY/AID/news.php?news=

    Have to modify the URL you want displayed by adding the code after "/", for example. Could be a hyphen, etc., but the regex has to be modified accordingly.

    Here is an example entering: http://localhost/news/42/ to go to http://localhost/DIRECTORY/AID/news.php?news=42:

    RewriteEngine On
    RewriteRule ^news/([0-9]+)/?$ DIRECTORY/AID/news.php?news=$1 [NC,L]
    

    That's all you need. To test this example, insert this only code in news.php at http://localhost/DIRECTORY/AID/

    <?php
    if ( $_GET[ 'news' ] == '42' ) {
      echo  "HERE I AM<br /><br />";
    }
    ?>
    

    UPDATED according to OP description. Any name can be used instead of This_is_news:

    RewriteEngine On
    RewriteRule ^news/([0-9a-zA-Z-_]+)/?$ DIRECTORY/AID/news.php?news=$1 [NC,L]
    
    0 讨论(0)
  • 2021-01-17 22:18

    This is a problem about URL-Rewrite and String-to-Id Algorithm.

    Above all, please remember that what ever your url changes by rewrite module, the url in the browser bar always contains the post param like id or string(about your news).

    Now to the question. Our purpose is just to rewrite the Url:

    http://localhost/DIRECTORY/AID/news/this_is_article_title
    

    to:

    http://localhost/DIRECTORY/AID/news.php?a_id=24
    

    With the rewrite module, we can only rewrite it to:

    http://localhost/DIRECTORY/AID/news.php?title=this_is_article_title
    

    and here is the htaccess part, alrealdy tested on a htaccess-online-tester

    RewriteRule ^DIRECTORY/AID/news/([A-Za-z0-9_]+)$ DIRECTORY/AID/news.php?title=$1 [QSA,L]
    

    The remaining work is build a algorithm for mapping the title to the news which rewrite module cannot help us(especially you manually rewrite the url one by one). We can use php code below:

    <?php
    
    //write url reflect to news title.
    function build_news_url_by_title(){
        $query = "SELECT * FROM news ORDER BY id DESC LIMIT 5";
        $result = mysql_query($query) or die(mysql_error());
        while($row = mysql_fetch_array($result)){
            echo "<a href=\"news/".strtr(substr($row['title'], 0,26), " ", "_")."\">".substr($row['title'], 0,26)."</a>";
        }   
    }
    
    function get_news_by_title($title){
        $real_title = strtr($title, "_", " ");
        $query = "SELECT * FROM news WHERE title LIKE %".$real_title."% LIMIT 1";
        return mysql_query($query) or die(mysql_error());
    }
    
    $title = $_POST['title'];
    
    $news = get_news_by_title($title);
    
    //some code return the news
    ...
    

    So the url:

    http://localhost/DIRECTORY/AID/news/this_is_article_title
    

    can give us the news also.

    Finally, from the steps above, if we type url

    http://localhost/DIRECTORY/AID/news/this_is_article_title
    

    in the browser bar, it can be also give us the news. And it has none bussiness about the stupid ID. And Though, there maybe some bugs with the code above, do not put it on your product server.

    0 讨论(0)
  • 2021-01-17 22:19

    Place RewriteBase right after RewriteEngine On It will set up rewrite engine correctly before you start redirecting

    0 讨论(0)
  • 2021-01-17 22:22

    Unfortunately I'm unable to answer your question in PHP or Apache (although I am using a hand-rolled REST converter to create URL addresses on my current project), but from what I understand, you want your user to type out http://localhost/DIRECTORY/AID/article.php?a_id=24 and the address bar should end up like http://localhost/DIRECTORY/AID/news/this_is_article_title.

    I'm not entirely sure what benefits this provides for you, and please bear in mind my solution will NOT allow your end-user to type the RESTful URL and end up at the page with the QueryString address. You'd need some extra legwork to do this, and even more legwork to keep it in sync with the DB (I'd recommend a script that deseminates the RESTFUL URL, queries the DB for the topic then returns the ID or page content ... but that's an different Stack Overflow question for another day.

    SOLUTION My proposed solution requires HTML5 doctype and a very light sprinkling of Javascript.

    history.replaceState(null, "history title here", "news/this_is_article_title");
    

    What this does is it changes the URL in the address bar without triggering a page redirect, reload or anything else. This javascript can be dynamically written with your PHP so as the page is served, the address is updated. The higher up in the document it is, the faster the change.

    Here's a jsFiddle link: http://jsfiddle.net/uT3RP/1/

    Unfortunately they run the code in an iframe so it doesn't control the main address bar, so I included an alert displaying what the address bar location would say, for proof. It's not the most elegant of solutions. I wouldn't even recommend doing what you're doing without the failsafes of making sure the URL displayed can be used to get to the same page. Disclaimer aside ... your problem is solved with 1 line of js and a doctype change (if you aren't using HTML5).

    You can stop flogging poor ol' htaccess and get on with your project :)

    0 讨论(0)
  • 2021-01-17 22:28

    I cannot comment so I have to answer.

    Reading all answers and your question, it is not clear what you want. At least for me. You say, for example:

    http://localhost/DIRECTORY/AID/article.php?a_id=24 to

    http://localhost/DIRECTORY/AID/article/this_is_article_title

    But I guess that's not quite right, unless you want

    http://localhost/DIRECTORY/AID/article.php?a_id=24

    to be the URL entered in the browser address bar and if so, what would be the purpose of the redirection? Finally, any visitor would have to type precisely what you don't want them to type.

    My guess is that you want the friendly URL to be entered:

    http://localhost/DIRECTORY/AID/article/this_is_article_title , so the question should be the other way around:

    http://localhost/DIRECTORY/AID/article/this_is_article_title TO

    http://localhost/DIRECTORY/AID/article.php?a_id=24

    The next thing that does not seem to be clear, is what's displayed in the browser bar? The only answer is: The entered URL. No way it can show anything different.

    In short: If you want http://localhost/DIRECTORY/AID/article/this_is_article_title to show in the addres bar, that's what you have to enter. The real URL, the SUBSTITUTION (http://localhost/DIRECTORY/AID/article.php?a_id=24), is never shown and is never typed. That's what redirection is for.

    On the other hand, it is not clear either how the ID numbers provided by news.php are expected to be converted to strings like article/this_is_article_title. ¿Where are those strings, how many ID numbers are, what kind of algorithm or formula should be used to achieve that conversion, which of those IDs are 'root" as you mentioned in a comment and how can they be identified, etc.? You should elaborate more on this point because it seems improvised and incoherent with your previous comments.

    I might be wrong, of course. I am just guessing to try to help with your question.

    Please, geniuses, don't downvote this answer, read it. I am not trying to answer the question and I am really far from being a genius.

    0 讨论(0)
提交回复
热议问题