Htaccess [SOLVED]: .htaccess rewrite to remove extension and query string and make into folder structure but if page doesn't exist send to another page

Htaccess [SOLVED]: .htaccess rewrite to remove extension and query string and make into folder structure but if page doesn't exist send to another page

Home Forums htaccess Htaccess [SOLVED]: .htaccess rewrite to remove extension and query string and make into folder structure but if page doesn't exist send to another page

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36969

    Anonymous

    QuestionQuestion

    I currently have the an .htaccess (included below) which works wonderfully. It takes a user to the content.php page and shows them the page that is stored in the database. However, there are some pages which required me to hardcode and thus are listed with the .php extension. Is there any way to add a rewrite rule that will allow me to remove the extension and send the query string as directories.

    Example

    Current: http://www.example.com/dashboard.php?ax=clients&do=save&miscthirdparam=doitnow

    To: http://www.example.com/dashboard/clients/save/doitnow/

    However, if the dashboard.php page doesn’t exist, then just send it to content.php as it does now to check if there is a page in the database to display? (I have an Error 404 show if there is no page in the db).

    **My Current .htaccess **

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^([a-zA-Z]*)/(.*)$ /content.php?url=$1/$2 [L]
        Options +FollowSymLinks
        RewriteEngine on
    </IfModule>
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /content.php?err=404
    </IfModule>
    

    #36970

    Anonymous

    Accepted AnswerAnswer

    You can use RewriteCond backreference. We can extract the first URL path, and find out if the php file exists. Given your example, we extract dashboard and check if %{DOCUMENT_ROOT}/dashboard.php file exists. If so, rewrite the url with specified paramaters. The regex [^/]* matches all characters except followed by forward slash /.

    RewriteCond %{REQUEST_URI} ^/([^/]*)
    RewriteCond %{DOCUMENT_ROOT}/%1.php -f
    RewriteRule ^([^/]*)(/([^/]*))?(/([^/]*))?(/([^/]*))? /$1.php?ax=$3&do=$5&miscthirdparam=$7 [L]
    

    Source: https://stackoverflow.com/questions/48029681/htaccess-rewrite-to-remove-extension-and-query-string-and-make-into-folder-stru
    Author: Ben
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.