Jump to content

Rwriting pages like blog permalinks


graham23s

Recommended Posts

Hi Guys,

 

I have moved from blogs to css/html sites again, but i liked the look of the permalink structure on blogs, for example:

 

my: /privacy-policy.php looks better as /privacy-policy/ i'm not sure what to write to make them look like the second option.

 

any help would be appreciated

 

thanks guys

 

Graham

Link to comment
https://forums.phpfreaks.com/topic/198913-rwriting-pages-like-blog-permalinks/
Share on other sites

Just make sure all your links are the second format not the first. Then to make them actually work using something like the following in a .htaccess file.

 

RewriteCond %{REQUEST_FILENAME } !-d
RewriteCond %{REQUEST_FILENAME } !-f
RewriteRule ^[a-zA-Z-]$ /$1.php [QSA]

Hi Cags,

 

Thank for the reply mate :)

 

i did a test on my localhost i made 2 files:

 

index.php

 

<a href="privacy-policy/">Privacy Policy</a>

 

privacy-policy.php

 

<h1>Privacy Policy Page</h1>

 

then my .htaccess file:

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME } !-d
RewriteCond %{REQUEST_FILENAME } !-f
RewriteRule ^[a-zA-Z-]$ /$1.php [QSA]

 

The strange thing is the directory doesn't show up at all, i took away the .htaccess file and found out the error: Internal Server Error was being returned i can't figure what the problem is.

 

thanks mate

 

Graham

Hi graham23s,

 

I was obviously half asleep when I wrote that code out. The 500 server error is probably caused by the spaces after REQUEST_FILENAME, not quite sure why I put those there, just from typing out I guess. Secondly I missed a quantifier off of the Regex pattern meaning it will only match URLs that are a single character long, thirdly there's not capture group so $1 will be an empty string meaning you will be redirected to http://domain.com/.php. Try this instead...

 

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z-]+)$ /$1.php [QSA]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.