1

i'm using an ubuntu LAMP and i'm trying to use mod_rewrite to convert url such as:

  • site/XXX to site/XXX.php and
  • site/XXX-YYY to site/XXX.php?sub1=YYY

I've used this rewrite rules:

RewriteRule ^([a-zA-Z0-9]+)$ ./$1.php
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2

In local on my OSX they works fine, anyway when uploaded the .htaccess on the ubuntu server, calling site/test i can't reach site/test.php but i get a 404. Mod_rewrite is working, in fact the 404 page is setted in the same .htaccess and works. In local i used this in a folder: localhost/myfolder/.... while in remote i'm using the root of a subdomain: app.sitename.com/....

Can someone help me?

This is the complete code:

RewriteEngine On
RewriteRule ^altro&(.+)? ./altro.php?$1 [L,QSA,NC]
RewriteRule ^([a-zA-Z0-9]+)$ ./$1.php [L,QSA,NC]
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2&sub2=$3
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2&sub2=$3&sub3=$4
RewriteRule ^([a-z]+)-([a-z]+)-([0-9]+)-([a-zA-Z0-9_]+).([a-zA-Z]+)$ ./server/php/files/$1/$2/$3/$4.$5

The first directive is working! In fact site/altro&code opens altro.php?code

anubhava
  • 761,203
  • 64
  • 569
  • 643
xspecial
  • 537
  • 1
  • 6
  • 17

2 Answers2

1

Maybe this helps to debug:

RewriteLog "/var/log/apache2/rewrite.log"
RewriteLogLevel 3

source: How to debug Apache mod_rewrite

(You cannot put this in .htaccess. You have to put it in the VirtualHost configuration.)

Community
  • 1
  • 1
Lajos Veres
  • 13,595
  • 7
  • 43
  • 56
  • I've tried and i got this at the end: 87.14.78.188 - - [22/Oct/2013:21:53:07 +0200] [app.XXX.it/sid#7ffec18566b0][rid#7ffec16a10a0/subreq] (1) [perdir /var/www/app/] pass through /var/www/app/test.php That's seems correct!! The file test.php exists and it is in that folder! Anyway i'm getting: Not Found The requested URL /test was not found on this server. – xspecial Oct 22 '13 at 19:53
0

Can you try this code:

RewriteEngine On

RewriteRule ^([a-zA-Z0-9]+)/?$ $1.php [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)/?$ $1.php?sub1=$2 [L,QSA]

Full .htaccess code:

Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^altro&(.+) altro.php?$1 [L,QSA,NC]
RewriteRule ^([a-zA-Z0-9]+)$ $1.php [L]
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ $1.php?sub1=$2 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ $1.php?sub1=$2&sub2=$3 [L,QSA]
RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ $1.php?sub1=$2&sub2=$3&sub3=$4 [L,QSA]
RewriteRule ^([a-z]+)-([a-z]+)-([0-9]+)-([a-zA-Z0-9_]+).([a-zA-Z]+)$ server/php/files/$1/$2/$3/$4.$5 [L,QSA]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Using: RewriteRule ^([a-zA-Z0-9]+)-?$ $1.php [L,QSA] it works but i'd like to call directly: site/pagename and not site/pagename/ or site/pagename- – xspecial Oct 22 '13 at 18:50
  • No, it doesn't works. Neither pagename/ or pagename Only if i use the "-" it works with pagename- Really don't understand. – xspecial Oct 22 '13 at 19:33
  • Can you post your latest & complete .htaccess in your question. – anubhava Oct 22 '13 at 20:02
  • Yes! Here it is: `RewriteEngine On RewriteRule ^altro&(.+)? ./altro.php?$1 [L,QSA,NC] RewriteRule ^([a-zA-Z0-9]+)$ ./$1.php [L,QSA,NC] RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2 RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2&sub2=$3 RewriteRule ^([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)-([a-zA-Z0-9]+)$ ./$1.php?sub1=$2&sub2=$3&sub3=$4 RewriteRule ^([a-z]+)-([a-z]+)-([0-9]+)-([a-zA-Z0-9_]+).([a-zA-Z]+)$ ./server/php/files/$1/$2/$3/$4.$5` The first directive is working! In fact site/altro&code opens altro.php?code – xspecial Oct 22 '13 at 20:15
  • Oh its impossible to read it from comments. Edit your question and post this code there. – anubhava Oct 22 '13 at 20:17
  • Nope. http://app.XXXX.it/test-1 works well but http://app.XXXX.it/test doesn't works: Not Found The requested URL /test was not found on this server. – xspecial Oct 22 '13 at 20:47
  • Q1. Is this the only .htaccess or do you have another? Q2. Is this the only code in this .htaccess? If you have some other code then please post it in question? Q3. What is location of above .htaccess? – anubhava Oct 22 '13 at 20:48
  • It is pretty late so I am going offline. I will check your comments t=in the morning. – anubhava Oct 22 '13 at 20:48
  • Q1. It's the only one Q2. Yes it is. I've pasted your exactly code. Q3. It is located in /var/www/app that is the folder refered by apache for app.site.it/ THanks – xspecial Oct 22 '13 at 20:51
  • DOCUMENT_ROOT is `/var/www` OR `/var/www/app` ? – anubhava Oct 22 '13 at 20:55
  • the document root is /var/www/app in the virtualhost app.site.it – xspecial Oct 22 '13 at 20:59
  • OMG i found that if i request an unexistent page such as: site.it/blabla it says: The requested URL /blabla.php was not found on this server. But ONLY if i use a real filename it doesn't rewrite the url!!! – xspecial Oct 22 '13 at 21:01
  • Perfect, glad it worked out. Consider accepting the answer now – anubhava Oct 22 '13 at 21:11