Sunday, March 24, 2019

apache - mod_rewrite convert: XXX-YYY to XXX.php?sub1=YYY



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


Answer



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]

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...