.htaccessmod-rewriteurl-rewritingdynamic-urlhtmlextensions

htaccess for Dynamic URL Rewrite with .html extension based on url selection


I'm trying to rewrite some dynamic page url in which data is taken from database with slug like "this-is-a-link".

        Options +FollowSymlinks
        RewriteEngine On

        RewriteBase /testweb/

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

It works fine with http://www.test.com/testweb/this-is-a-link

But I need to change the rewrite rule based on any link clicked as below.

http://www.test.com/testweb/faq/this-is-a-link, 'testweb/news/this-is-another-link','/testweb/testimonials/test-link'

The "faq_pages.php" in the above RewriteRule should be changed to "news_pages.php". How can I have it dynamically so that I can use for every page with same htaccess or how can I check and rewrite the value to different pages in htaccess.

Also please advice me how to correct below code.

        Options +FollowSymlinks
        RewriteEngine on
        RewriteRule ^(.*)\.html$ faq_pages.php?page=$1

I need to show html urls in the address bar like '/testweb/this-is-a-link.html' or '/testweb/faq/this-is-a-link.html'

Edit: By the help of Jon Lin I have updated my codes as below.

        Options +FollowSymlinks
        RewriteEngine On

        RewriteBase /testweb/

        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^faq/(.*?)/?$ faq_pages.php?page=$1 [QSA]

Now both below conditions are working : /testweb/faq/another-faq-test.html,
/testweb/faq/another-faq-test.html/

As I don't have any sub-folder with name 'faq' the template files(images/css/js etc) in root folder are not loading. How can I fix it?

Thanking you


Solution

  • Are you looking for something like this?

        Options +FollowSymlinks
        RewriteEngine On
    
        RewriteBase /testweb/
    
        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^faq/(.*)$ faq_pages.php?page=$1 [QSA,L]
    
        RewriteCond %{REQUEST_FILENAME} !-d [NC]
        RewriteCond %{REQUEST_FILENAME} !-f [NC]
        RewriteRule ^news/(.*)$ news_pages.php?page=$1 [QSA,L]