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
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]

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.