SitePoint Sponsor

User Tag List

Results 1 to 21 of 21

Thread: mod_rewrite

  1. #1
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mod_rewrite

    Hi All,

    I'm using Apache 2.2.8 on my VPS and I have the rewrite module enabled. I need to hide some $_GET variables in the URL ($_POST is out of option, as I have links with passed variables). I found a lot of tutorials and articles and created this httpd.conf :
    Code:
    RewriteEngine On
    RewriteRule ^(.+)/([0-9]+)/?$ /var/www/mysite/my_page.php?referrer=$1&m_id=$2
    I've uploaded the httpd.conf to /etc/apache2 directory, restarted Apache, but nothing happened. Can someone take me to the right direction? Thanks!

    Full time ADMIN - art community
    Part time coder - dsign

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    Duj,

    WHERE did you put that code?

    WHY did you use the physical address (you MUST use the WEBSPACE address)?

    WHY did you not use the Last flag?

    Regards,

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

  3. #3
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi David. Thank you for trying to help me...
    Well, first I put the above code lines into the httpd.conf in the /etc/apache2 folder. Restarted Apache but no changes... After that I created a .htaccess file with that code lines and saved in the root folder of my site (/var/www/mysitesfolder).
    To be honest I don't understand terms such as WEBSPACE address... You mean http://www.mysite.whatever instead of /var/www..... ?
    What does LAST FLAG mean?

    P.S.: sorry for my freebie questions but I know almost nothing about configuring Apache....

    Full time ADMIN - art community
    Part time coder - dsign

  4. #4
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    duj,

    That's what I'm here for!

    I think your first step should be to read the tutorial article linked in my signature. It will tell you the mods necessary to your httpd.conf file. It will then explain how to create valid (okay, useful) regular expressions (regex), tell you about the mod_rewrite flags (the Last flag terminates a mod_rewrite block statement) and, finally, it offers sample code (from member questions over several years).

    Webspace is what a browser can look at (via a web server, i.e., Apache). The webspace address is NOT the same as the physical address of a file as the webspace is relative to the DocumentRoot of the domain (even if that's only localhost - which is typically located in Apache's htdocs folder).

    Regards,

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

  5. #5
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks David,
    I'll read your article right away!

    Full time ADMIN - art community
    Part time coder - dsign

  6. #6
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmmmm.....
    I got stopped at the first step......
    I uploaded the test.html, test.php and the .htaccess files :

    Code:
    RewriteEngine on
    RewriteRule ^/?test\.html$ test.php [L]
    as described at http://datakoncepts.com/seo
    With phpinfo() I checked the loaded modules and mod_rewrite is loaded!

    And still my browser is showing the annoying
    Code:
    This is the HTML file.
    message BUT it supposed to show the refreshing
    Code:
    This is the PHP file.
    What am I doing wrong?

    Full time ADMIN - art community
    Part time coder - dsign

  7. #7
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    duj,

    You must've missed the

    <Directory />
    Options FollowSymLinks
    AllowOverride All
    </Directory>

    Regaards,

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

  8. #8
    SitePoint Member Blue Chocolate's Avatar
    Join Date
    Oct 2009
    Location
    San Jose
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have tried out module re-wriitng a couple of times myself, but I still haven't get the hang of it yet
    Top Rated Social Networking Site- 7th Billion!
    Lovin' the open source social network group

  9. #9
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    <Directory />
    Options FollowSymLinks
    AllowOverride All
    </Directory>
    That's in /etc/apache2/apache2.conf CORRECT?

    Full time ADMIN - art community
    Part time coder - dsign

  10. #10
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    <Directory />
    Options FollowSymLinks
    AllowOverride All
    </Directory>
    I put that into /etc/apache2/httpd.conf, have restarted Apache2, the browser is still displaying
    Code:
    This is the HTML file.
    ......
    Did I miss something more?

    Full time ADMIN - art community
    Part time coder - dsign

  11. #11
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    duj,

    In the WinDoze version, it's in the httpd.conf (but that may different in the Linux version).

    Failing there, try to put it into the .htaccess file without the directory brackets.

    Regards,

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

  12. #12
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have Linux Ubuntu 8.04.2 on my Virtual private server running Apache 2.2.8 and PHP 5.2.4

    I put
    Code:
    <Directory />
    Options FollowSymLinks
    AllowOverride All
    </Directory>
    in /etc/apache2/httpd.conf and then in /etc/apache2/apache2.conf and then in .htaccess in the root web-folder, and still, rewrite rule doesn't work at all.......
    What should I do next?

    Full time ADMIN - art community
    Part time coder - dsign

  13. #13
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    duj,

    httpd.conf is the only place where that's needed. In my test server's httpd.conf, I have:
    Code:
    # First, we configure the "default" to be a very restrictive set of 
    # features.  
    #
    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Order deny,allow
        Deny from all
    </Directory>
    ... and it works perfectly.

    You may also need:
    Code:
    RewriteEngine on
    # followed by your mod_rewrite statements
    Regards,

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

  14. #14
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe DK that it's working perfectly at your side, for some (to me UNKNOWN) reason rewrite engine simply REFUSES to work at my side....

    Here's a listing of the loaded modules :
    Code:
    Loaded Modules core mod_log_config mod_logio prefork http_core mod_so mod_alias mod_auth_basic
    mod_authn_file mod_authz_default mod_authz_groupfile mod_authz_host mod_authz_user
    mod_autoindex mod_cgi mod_dir mod_env mod_mime mod_negotiation mod_php5
    mod_rewrite mod_setenvif mod_status
    Seems OK to me.....
    I'm giving up......

    Full time ADMIN - art community
    Part time coder - dsign

  15. #15
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    duj,

    Aw, don't give up!

    Go back and check everything!

    1. mod_rewrite enabled. While your phpinfo() shows it is, your test shows it isn't. Can you check the httpd.conf file (just download to see what it says about your server - you CAN do this with your VPS OR have your management support team do it for you) to make SURE that the mod_rewrite line is uncommented.

    2. Ditto the above to see that you have both Options FollowSymLinks and AllowOverride All statement (at least in the httpd-vhosts.conf).

    3. If you've added the RewriteEngine on statement BEFORE your test mod_rewrite code, then add the R=301 flag so you can see whether your mod_rewrite is working or not.
    Code:
    RewriteEngine on
    RewriteRule ^test\.html$ test.php [R=301,L]
    Note that this will ONLY work in your domain's DocumentRoot (where the .htaccess is located, i.e., http://yourdomain.com/test.html). The location should be changed by mod_rewrite to http://yourdomain.com/test.php.

    As for your original mod_rewrite code (for when you get past the test to assure yourself that mod_rewrite is both installed and enabled):
    Code:
    RewriteEngine on
    RewriteRule ^(.+)/([0-9]+)/?$ my_page.php?referrer=$1&m_id=$2 [L]
    NOTE: my_page.php will HAVE to have the <base> tag set to use relative links because of the optional trailing slash.

    Regards,

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

  16. #16
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post

    1. mod_rewrite enabled. While your phpinfo() shows it is, your test shows it isn't. Can you check the httpd.conf file (just download to see what it says about your server - you CAN do this with your VPS OR have your management support team do it for you) to make SURE that the mod_rewrite line is uncommented.
    Thank you DK you're not giving up on me!
    Which exactly httpd.conf file do you mean?
    At the /etc/apache2/httpd.conf I only have
    Code:
    <Directory />
        Options FollowSymLinks
        AllowOverride All
        Order deny,allow
        Deny from all
    </Directory>
    and nothing else...

    Full time ADMIN - art community
    Part time coder - dsign

  17. #17
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,313
    Mentioned
    15 Post(s)
    Tagged
    2 Thread(s)
    duj,

    I'm too stupid (or tenacious) to give up! If you have a problem, I'll get you through it!

    WHERE is Apache located? Do a WHEREIS httpd.conf 'cause the <Directory> block is the least of what's in that file. LOOK AT ALL THAT WILL BE LISTED (I can't believe there'd be more than one).

    Regards,

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

  18. #18
    i want cake and cookies Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    9,995
    Mentioned
    41 Post(s)
    Tagged
    1 Thread(s)
    Hi dujmovicv,

    I am also running this version of Ubuntu. You are using the wrong file for httpd...

    I also got that file, and it was empty after installing Apache2 (and it's still empty). The "real" config file is
    /etc/apache2/apache2.conf

    so in your terminal do
    Code:
    vi /etc/apache2/apache2.conf
    and what you SHOULD see is a bunch of commented out stuff. It starts out a full file.

    Here's what I ended up doing, because I was imitating my work's VirtualHost setup:

    in /etc/apache2/apache2.conf at the BOTTOM I think I UNcommented an Include, so that all my messing around wasn't in the main config file itself but another file that the main config looks at:

    Quote Originally Posted by main config
    # Include the virtual host configurations:
    Include /etc/apache2/sites-enabled/ <--note this is where you'll do the stuff dkLynn is talking about, with <Directory> and your mod_rewrite commands!
    When you say "Include path/file" you're telling the main config to act as if you wrote it directly in there, but really it's safely in another file.

    You already have the /var/www thing, great.

    Now go check out /etc/apache2/sites-enabled and you should see something called "default" or maybe default with some 0's in there. I'm not sure if the 0's are an Ubuntu thing or maybe an Apache thing? We can refer to the file as "default" though.

    Code:
    vi /etc/apache2/sites-available/default
    (or whatever your text editor of choice is)
    There should already be a bunch of text in there, including your <Directory> stuff.
    Quote Originally Posted by default
    NameVirtualHost *
    <VirtualHost *>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www/
    <Directory />
    Options FollowSymLinks
    AllowOverride None
    </Directory>
    <Directory /var/www/>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    allow from all
    </Directory>

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined
    ServerSignature On

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride None
    Order deny,allow
    Deny from all
    Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

    RewriteEngine On
    RewriteLog /var/log/apache2/rewrite.log

    RewriteLogLevel 5

    RewriteRule (put your rewrite rules here)
    </VirtualHost>
    That's any Named virtual host, <virtualhost any>
    that Document Root /var/www is why when in your regexes you have / (root) it knows you really mean /var/www/thefilesarehere
    I think everything in the middle was already there...
    then from "Rewrite engine on" is all added. You turn it on, state where logs are to go, loglevel, etc. I set mine high.

    So when I was testing basic mod_rewrite I did this (I don't have PHP, so it was simpler):
    in /var/www
    I have
    index.html
    about.html
    products.html
    contact.html
    each with h1's clearly stating the name of the file (so like you you have with your index.html and .php files)

    And a test rewrite was
    Code:
            RewriteRule ^/index(\.html)?$ /about.html [L]
            RewriteRule ^/about(\.html)?$ /index.html
    Where I find by trial-and-error that it only worked if I start with ^/ for the pattern and / before the real file ("/about.html" is referring to "/var/www/about.html" because that's what you've got stated for / in your "default" file).

    Because this isn't an .htaccess file, every time I edit this default file, I then need to
    Code:
    sudo /etc/init.d/apache2 reload
    NOT the reload commands you see at apache.org (cause Debian just HAD to be different and weird, didn't they?? this caused me a lot of grief)
    try to test your pages and rewrites in your web browser
    Code:
    http://localhost/index.html
    for example shows about.html

    then check your new log ends (the log will get huge fast, so I tail it)
    Code:
    tail /var/log/apache2/rewrite.log
    to see what the logs say about your rewrite.

    Hope that helped. Dwebian makes Apache a pain when you're getting started. Rich Bowen calls it "Debian-encumbered" lawlz.

  19. #19
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Stomme poes View Post
    Hi dujmovicv,

    I am also running this version of Ubuntu. You are using the wrong file for httpd...

    I also got that file, and it was empty after installing Apache2 (and it's still empty). The "real" config file is
    /etc/apache2/apache2.conf

    ...
    Thank you STOMME!!!! That's a very detailed help! And thanks to dklynn as well! Without you guys it would be really HARD to make this work! I think I'm getting closer now!
    I'm trying to make now my specific rewrite rule and put it into .htaccess!

    Thanks again!
    Have a nice day!

    Full time ADMIN - art community
    Part time coder - dsign

  20. #20
    i want cake and cookies Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    9,995
    Mentioned
    41 Post(s)
    Tagged
    1 Thread(s)
    I'm trying to make now my specific rewrite rule and put it into .htaccess!


    Then be aware, my example said I needed the leading slash to get anything to work-- but I'm working with the config file. With .htaccess, you don't use a leading slash in your regex pattern.

    Quote Originally Posted by me
    Where I find by trial-and-error that it only worked if I start with ^/ for the pattern and / before the real file ("/about.html" is referring to "/var/www/about.html" because that's what you've got stated for / in your "default" file).
    This will not be true with .htaccess in a virtual host.

  21. #21
    a fresh, new start... dujmovicv's Avatar
    Join Date
    Aug 2006
    Location
    Earth
    Posts
    556
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for the warning Stomme! Actually I managed to create a rewrite rule, put it into .htaccess
    Code:
    RewriteEngine on
    RewriteRule ^/?messages/([^/\.]+)/([0-9]+)/?$ view_message.php?referrer=$1&m_id=$2 [L]
    and it worked OK, unless ALL the links on the above mentioned page have to be modified from relative to absolute path (or vice versa???)...

    BUT, the other day (that's today) I'm experiencing problems with my server....
    I can't load any page that uses MySql database support, I'm getting the error message at the browser :
    Code:
    Fatal error: Cannot redeclare rhse() (previously declared in /var/www/mydomain/index.php(1) : eval()'d code:1) in /var/www/mydomain/include/database.php(1) : eval()'d code on line 1
    I don't know if that's an Apache issue, MySql or what..... I tried to restart MySql server, Apache2 as well, without result......

    Did I screw up my server with the mod_rewrite and rewrite_rules ?

    Full time ADMIN - art community
    Part time coder - dsign

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
  •