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 Answer 1

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]
Sign up to request clarification or add additional context in comments.

6 Comments

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 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
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 Use the regex: ^faq/(.*?)/?$. And format the code in your question or something, it's impossible to read in comments
Great.. now the html/ url is also working. I will edit the question. Thank you
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.