URL Rewriting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ian
    Member
    • Mar 2004
    • 64

    #1

    URL Rewriting

    I can't get URL rewriting to work for the life of me. I have seen some of the threads on this forum and have something like the snippet below, but it doesn't work.

    RewriteEngine On
    RewriteRule ^(.*)$ index.php?sec=$1 [L,QSA]

    Basically I want my index.php?sec= to be replaced with the query string value, so it would be mydomain.com/about.

    Any ideas?
    www.ianhoar.com - A place to geek out
  • djn
    Senior Member
    • Mar 2004
    • 140

    #2
    I guess your request is getting stuck in a loop. I'd suspect the rule is being indeed applied as expected, but then the rewritten request is being sent again to the RewriteEngine and the rule reapplied to it.
    I believe a RewriteCond is called for here, preventing the rule to be applied to requests for index.php.
    As a more general advice I'd enable mod_rewrite logging to see what exactly is being processed, too.

    Comment

    • ZYV
      Senior Member
      • Sep 2005
      • 315

      #3
      What exactly does not work the way you want it to?

      Originally posted by Ian
      Basically I want my index.php?sec= to be replaced with the query string value, so it would be mydomain.com/about.
      Your regular expression only applies to the relative URL path so there is no way it can give you "mydomain.com/about". What you will get is just "about" in this example. If you want the host, it might be easier to fish it out of the $_SERVER[] superglobal or alternatively use %{HTTP_HOST} or whatever it is environment variable.

      Comment

      • Ian
        Member
        • Mar 2004
        • 64

        #4
        I'm not sure I follow, but I've tried just about every RewriteRule I can find on the web and none of them have worked for me.
        www.ianhoar.com - A place to geek out

        Comment

        • Ian
          Member
          • Mar 2004
          • 64

          #5
          Okay, some progress. I don't understand why so many of the examples I've tried from the web do not work, but this one does.

          Options +FollowSymlinks
          RewriteEngine on
          RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?sec=$1
          RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?sec=$1

          Now the problem is if you access my site with



          But if you use



          the images do not load.

          So is there a way to rewrite the image paths in .htaccess. Also, this only works if you type the new URL's directly. I guess I need some kind of redirect too.
          www.ianhoar.com - A place to geek out

          Comment

          • ZYV
            Senior Member
            • Sep 2005
            • 315

            #6
            I have no idea why the first example in the thread does not work for you. You are clearly doing something wrong, but you don't want to get into the details for your results to be reproducible by others. There is no way anybody can help you unless one can reproduce your problem and therefore I really don't understand what kind of feedback you are expecting to obtain.

            .htaccess
            RewriteEngine On
            RewriteRule ^blah(.*)$ blah.php?sec=$1 [L,QSA]
            blah.php
            <?php echo $_GET["sec"]; ?>


            Everything works perfectly fine for me.

            As for the last example, for course the images won't load. Use image paths relative to the root (/img/blah.png) or <base> tag in your markup.

            Comment

            • Ian
              Member
              • Mar 2004
              • 64

              #7
              Well, it helps to know that it works for you, no one said it was correct.

              I looked at your example. My second example is working, but I want it to do this automatically even if you type in index.php?sec=blah and then just redirects to /blah. Everything I've looked at says this is possible, but I have not been able to duplicate it.
              www.ianhoar.com - A place to geek out

              Comment

              • ZYV
                Senior Member
                • Sep 2005
                • 315

                #8
                Originally posted by Ian
                Everything I've looked at says this is possible, but I have not been able to duplicate it.
                What seems to be the problem?


                Comment

                • Camo.Fish
                  Member
                  • Jul 2008
                  • 42

                  #9
                  lol

                  i just posted this in another topic, but here it is again:

                  .htaccess
                  RewriteEngine On
                  # Remove www.
                  RewriteCond %{HTTPS} on
                  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
                  RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
                  RewriteCond %{HTTPS} off
                  RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
                  RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
                  # Translate Semantic to Get
                  RewriteCond %{SCRIPT_FILENAME} !-f
                  RewriteCond %{SCRIPT_FILENAME} !-d
                  RewriteRule ^(.*) /index.php?sec=$1 [L,QSA]


                  the QSA is query string append, so if you have any get variables they will still work. you can see this in action @ http://lookintomycode.com or http://a-little-taste-of-j.com (only pages starting with A-L have content thus far) has some examples of using /s in the semantic url on the deeper pages.

                  Comment

                  Working...