0

I am having some problems with mod rewrite. The server is Ubuntu 12.04, Apache2, Php 5.4.

I want an url like this:

www.website.com/<modelName>/<imageNumber>

And

www.website.com/<modelName>

To actually show:

www.website.com/showimage.php?model=<modelName>&image=<imageNumber>

And

www.website.com/model.php?model=<modelName>

I have put this in the .htaccess:

RewriteEngine On
RewriteRule ^([^/]+)/([0-9]+)/?$   showimage.php?model=$1&image=$2  [R,L,NC,QSA]
RewriteRule ^([^/]+)/?$            model.php?model=$1               [R,L,NC,QSA]

I have checked that mod_rewrite is loaded in apache. But the page just doesn't redirect.

EDIT: Just to clarify, the result is a 404 'the resource / does not exist on this server' and 'the resource // does not exist on this server'.

The strange thing is that I was playing around with it yesterday and tried to make a rule that included website.com/model/modelName and that seem to be in effect now and impossible to get rid of. I have tried to restart Apache ten times, clear browser caches and even install another browser that I didn't have before but the die-hard redirect from yesterday keeps going even in the new browser (FireFox).

Are the new redirect rules that I have written correct?

How do I put them in action?

Should I set chmod 777 on .htaccess?

Thanks a lot...

EDIT: here is some more information.

I only have one (default) site and the files (including .htaccess) are just sitting in /var/www

The file /etc/apache2/sites-available/default looks like this:

 <VirtualHost *:80>
    ServerAdmin webmaster@localhost

    DocumentRoot /var/www
    <Directory />
            Options FollowSymLinks
            AllowOverride All
    </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 ${APACHE_LOG_DIR}/error.log

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

    CustomLog ${APACHE_LOG_DIR}/access.log combined

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>

I haven't changed anything there except Directory / AllowOverride All

.htaccess looks like this:

RewriteEngine On
RewriteLog "/etc/apache2/logs/rewrite.log"
RewriteLogLevel 9
RewriteBase /
RewriteRule ^([^/]+)/([0-9]+)/?$   showimage.php?model=$1&image=$2  [R,L,NC,QSA]
RewriteRule ^([^/]+)/?$            model.php?model=$1               [R,L,NC,QSA]

And I have run service apache2 restart endless times.

This url works fine:

/showimage.php?model=Ai&image=12

This one doesn't:

/Ai/12

HELP!

0

1 Answer 1

1
RewriteEngine On
RewriteRule ^([^/]+)/([0-9]+)/?$   showimage.php?model=$1&image=$2  [R,L,NC,QSA]
RewriteRule ^([^/]+)/?$            model.php?model=$1               [R,L,NC,QSA]

Rather than going "this should work", I'll leave it to you by just translating the rules:

  • The first rule will match [anything]/[digits] with or without a trailing slash and re-map to showimage.php?model=$1&image=$2 using a redirect, will break the chain (i.e. the second rule won't be executed), is case-insensitive (not needed), and preserves query string arguments. The second does the same, but model.php.

It is important to note that the second rule does not match an empty string (i.e. fetching /). This might be part of your problem.

The rules themselves are correct. You can test these on the rewrite tool at: http://martinmelin.se/rewrite-rule-tester/ .

What I believe your problem is is pretty simple. Please answer the following questions in a comment and I'll advise from there:

  • Are you trying to fetch / without an index? If so, neither of your rules is matching it and that is why you're getting a 404.
  • If not, what exact URL are you trying to fetch? Do you have access to server logs? If so, can you turn on the rewrite log?
23
  • Hi, thanks for your help. I am not getting a 404 with an "empty" url i.e. www.website.com. I am getting the front-page, which is fine. But requesting e.g. www.website.com/name/123 gives a 404 whereas www.website.com/showimage.php?model=name&image=123 works fine.
    – Jesper
    Commented May 2, 2013 at 22:35
  • Can you add before the rewrite rules the following line: RewriteBase /. Let me know if it does anything. Commented May 2, 2013 at 22:36
  • I tried turning on the rewrite log by adding this into .htaccess: RewriteEngine On RewriteLog "/etc/apache2/logs/rewrite.log" RewriteLogLevel 9 RewriteRule ^([^/]+)/([0-9]+)/?$ showimage.php?model=$1&image=$2 [R,L,NC,QSA] RewriteRule ^([^/]+)/?$ model.php?model=$1 [R,L,NC,QSA] ...But the folder /etc/apache2/logs remains empty
    – Jesper
    Commented May 2, 2013 at 22:37
  • The folder remains empty? Okay. either you're running suhosin or your htaccess override is completely off. Go to your virtualhost config and add the following index to your options on the directories: AllowOverride all. This will turn htaccess back on if it is that problem. If it isn't, it won't do anything. More info at httpd.apache.org/docs/2.2/mod/core.html#allowoverride Commented May 2, 2013 at 22:39
  • I added the RewriteBase / in the file, but no change. Please note that /model/<modelName> still works, even in "incognito mode" in Chrome and on machines/browsers that have never loaded the website before
    – Jesper
    Commented May 2, 2013 at 22:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.