September 13, 20223 yr Hi all. I'm having a issue with rewriting a rule for my ht access where I have have a simple slug work for my website I'm building Here is my url for a dynamic page I have: dev.mysite.com/views/blog/page.php?page_slug=post-oneI'm trying to get it so I can add the URL in the address bar so this would work: dev.mysite.com/blog/post-one Here is the current .htaccess RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [QSA,L] Any heklp most apprciated.
September 14, 20223 yr This should work: RewriteEngine on RewriteRule ^/(.*)/(.*)$ /views/$1/page.php?page_slug=$2 [L,NC,QSA] If you don't want any query string to be appended to the URL then you don't have to include the 'QSA'.
January 22, 20233 yr Here's one possible way to rewrite the rule in your .htaccess file to achieve the desired URL structure: RewriteEngine On RewriteRule ^blog/([a-zA-Z0-9-]+)/?$ views/blog/page.php?page_slug=$1 [NC,QSA,L] This rule will match any URL that starts with "blog/" followed by one or more characters (alphanumeric or a dash) and an optional trailing slash. The matched string will be passed as the value of the "page_slug" query parameter to the "views/blog/page.php" file. The "NC" flag stands for "No Case" makes the pattern matching case-insensitive. The "QSA" flag stands for "Query String Append" it will append any existing query parameters to the rewritten URL. The "L" flag stands for "Last" it tells Apache to stop processing any more rules if this one is a match. Please note that this is only one possible solution and it may not work for your specific case. It's also important to make sure that the .htaccess file is located in the root directory of your website and that the Apache mod_rewrite module is enabled.
Create an account or sign in to comment