apache.htaccessmod-rewriteurl-rewritingfriendly-url

How to write a clean URL with mod_rewrite using virtual dirs, actual dirs and variables as a combination


I have read prior questions and read tutorials and I do not understand mod_rewrite fully so I am posting here. My current .htaccess is:

Options +FollowSymLinks
RewriteEngine On

#### 1st set of rules.... ####
#Using THE_REQUEST for catching references and using them later on.
RewriteCond %{THE_REQUEST} \s/(content)/index\.php\?var=(.*?)\s [NC]
RewriteRule ^ /%1/featured/%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(content)/featured/(.*?)/?$ $1/index.php?var=$2 [QSA,NC,L]


#### 2nd set of rules.... ####
#Using THE_REQUEST for catching references and using them later on.
RewriteCond %{THE_REQUEST} \s/(profile)\.php\?userid=(.*?)\s [NC]
RewriteRule ^ /%1%2? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(profile)/(.*?)/?$ $1.php?userid=$2 [QSA,NC,L]

I have this URL: www. domain .com/content/cat/build and it points to www. domain .com/content/catcontent.php?catvar=build ?? what htaccess mod_rewrite rule would it follow?

I have added this but it breaks the site...

   #### 3rd set of rules.... ####
    #Using THE_REQUEST for catching references and using them later on.
    RewriteCond %{THE_REQUEST} \s/(content)/(cat)/\catview\.php\?cattype=(.*?)\s [NC]
    RewriteRule ^ /%1%2? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(content)/(cat)/catview.php/?$ $1.php?cattype=$2 [QSA,NC,L]

Thanks

I have googled and read old posts. I have read tutorials. I do not understand mod_rewrite that well since it seems like an alphanumeric word salad and am asking for assistance


Solution

  • With your shown samples, please try following rules. Please make sure to clear your browser cache before testing your URLs

    Options +FollowSymLinks
    RewriteEngine On
    
    #### 1st set of rules.... ####
    #Using THE_REQUEST for catching references and using them later on.
    RewriteCond %{THE_REQUEST} \s/(content)/index\.php\?var=(.*?)\s [NC]
    RewriteRule ^ /%1/featured/%2? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(content)/featured/(.*?)/?$ $1/index.php?var=$2 [QSA,NC,L]
    ##############################
    
    #### 2nd set of rules.... ####
    #Using THE_REQUEST for catching references and using them later on.
    RewriteCond %{THE_REQUEST} \s/(profile)\.php\?userid=(.*?)\s [NC]
    RewriteRule ^ /%1%2? [R=301,L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(profile)/(.*?)/?$ $1.php?userid=$2 [QSA,NC,L]
    ##############################
    
    #### 3rd set of rules from here....
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(content)/(cat)/([^/]+)/?$ $1/$2$1.php?catvar=$3 [QSA,NC,L]