skip to Main Content

Hi I’m developing a website and all .php files are located in the main folder on the server (where .htaccess is).
So index.php and productDetail.php are in the same folder.
I want to redirect a url like this :

http://www.mywebsite/productDetail/PRODUCTNAME

to

http://www.mywebsite/productDetail.php?product=PRODUCTNAME

so that I can link the first link version instead of the second.

I also need to redirect a more complicated version, like:

http://www.mywebsite/PRODUCTCATEGORY/PRODUCTSUBCATEGORY/productDetail/PRODUCTNAME

to

http://www.mywebsite/productDetail.php?product=PRODUCTNAME&category=PRODUCTCATEGORY&subcategory=PRODUCTSUBCATEGORY

My .htaccess currently uses the following rules that I think are making really difficult this operation because I’ve been working on it a lot of time :

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

RewriteCond %{THE_REQUEST} !s/(it|en)/ [NC]
RewriteCond %{HTTP:Accept-Language} ^it [NC]
RewriteRule ^ /it%{REQUEST_URI} [L,NC,R=301]

RewriteCond %{THE_REQUEST} !s/(it|en)/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,NC,R=301]

RewriteRule ^(en|it)/(.*)$ $2?lang=$1 [L,QSA,NC]

Any help would be appreciated.
Thanks a lot

2

Answers


  1. You can use a Rule of the following form :

    RewriteEngine on
    
    
    RewriteRule ^productdetail/productname/?$ /productdetail.php?productname [NC,L]
    

    The Rule with Regex patterns :

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/]+)/([^/.]+)/?$ /productdetail.php?productname [NC,L]
    

    This will internally redirect /productDetail/foo to /productDetail.php?product?foo.

    Login or Signup to reply.
  2. Try these rules:

    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s([^.]+).php [NC]
    RewriteRule ^ %1 [R,L]
    
    RewriteCond %{THE_REQUEST} !s/(it|en)/ [NC]
    RewriteCond %{HTTP:Accept-Language} ^it [NC]
    RewriteRule ^ /it%{REQUEST_URI} [L,NC,R=301]
    
    RewriteCond %{THE_REQUEST} !s/(it|en)/ [NC]
    RewriteRule ^ /en%{REQUEST_URI} [L,NC,R=301]
    
    RewriteRule ^(en)/(productDetail)/([w-]+)/?$ $2.php?lang=$1&product=$3 [L,QSA,NC]
    RewriteRule ^(it)/(dettagliProdotto)/([w-]+)/?$ $2.php?lang=$1&product=$3 [L,QSA,NC]
    
    RewriteRule ^(en)/([w-]+)/([w-]+)/(productDetail)/([w-]+)/?$ $4.php?lang=$1&product=$5&category=$2&subcategory=$3 [L,QSA,NC]
    RewriteRule ^(it)/([w-]+)/([w-]+)/(dettagliProdotto)/([w-]+)/?$ $4.php?lang=$1&product=$5&category=$2&subcategory=$3 [L,QSA,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]
    
    RewriteRule ^(en|it)/(.*)$ $2?lang=$1 [L,QSA,NC]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search