1

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

1 Answers1

0

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]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Hi, Thanks for you answer. I have used RewriteRule ^faq/(.*)/$ faq_pages.php?id=$1 [QSA] and RewriteRule ^faq/(.*)$ faq_pages.php?id=$1 [QSA] for rewriting testweb/faq/another-faq-test.html/ and testweb/faq/another-faq-test.html . Please tell me if it is the right way to do it. Also please help me to ignore/rewrite the 'faq' folder because currently the css or images are not loading as there is a 'faq/' in the url. Thanks a lot – user2669827 Aug 20 '13 at 00:41
  • @user2669827 instead of using 2 rules, just add a `?` after the `/`. If you request a static file in `/faq/` the 2 rewrite conditions prevent the rule from being applied – Jon Lin Aug 20 '13 at 00:44
  • Hi., Thanks Again. RewriteRule ^faq/(.*)/?$ faq_pages.php?id=$1 [QSA] is not working for /testweb/faq/another-faq-test.html/ . I have used 'faq/' and 'news/' only for checking the url and rewrite to different pages. I don't have any folder with those names. How can I ignore it or specify that they are not directories.. now the url looks for a folder 'faq' – user2669827 Aug 20 '13 at 00:55
  • @user2669827 Use the regex: `^faq/(.*?)/?$`. And format the code in your question or something, it's impossible to read in comments – Jon Lin Aug 20 '13 at 00:56
  • Great.. now the html/ url is also working. I will edit the question. Thank you – user2669827 Aug 20 '13 at 01:01
  • @user2669827 your pages need to either have absolute paths to their images/css/js files, or you need to define the relative URI base: `` or whatever the base should be – Jon Lin Aug 20 '13 at 01:13