0

I have built a custom PHP, mySQL CMS. The news article links show as this:

https://www.example.com/news/post.php/?p=example-article-slug

However, i want them to show as this:

https://www.example.com/example-article-slug

The below code in the htacess file works but breaks all the other functionality in the htaccess such as the 404 and 301 redirects. Any ideas?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ news/post.php/?p=$1 [QSA,L]

Here's my full htaccess ...

            RewriteEngine On 
            RewriteCond %{SERVER_PORT} 80 
            RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

            Options -Indexes
            ErrorDocument 201 /404.php
            ErrorDocument 401 /404.php
            ErrorDocument 404 /404.php
            ErrorDocument 402 /404.php
            ErrorDocument 505 /404.php
            ErrorDocument 500 /404.php
            RewriteEngine On
            RewriteBase /

            Redirect 301 /oldpage.php   http://www.example.com/

            #Check for www
            RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
            RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

            RewriteCond %{THE_REQUEST} \ /(.*/|)(?:index\.php)?\?id=([^\s&]+) [NC]
            RewriteRule ^ /%1%2? [R=301,L,NE]

            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ news/post.php/?p=$1 [QSA,L]

            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ news/category/?cat=$1 [QSA,L]
1
  • Last rule for news/category/?... will never trigger because rule before that is sending every non-file/directory to news/post.php Commented Jan 18, 2017 at 15:06

1 Answer 1

2

You can use this:

RewriteEngine On
RewriteRule ^([^/]*)$ /news/post.php/?p=$1 [L]

It will leave you with this URL: https://www.example.com/example-article-slug.

Make sure you clear your cache before testing this.

Sign up to request clarification or add additional context in comments.

2 Comments

I used this which works, however it still knocks the rest of the htaccess rules out. I will post my entire htaccess now ...
The issue isn't with the rule above. It will be one of your other rules knocking it out. I advise commenting everything else out and begin to un-comment and test until you find the rule that is causing the issue. You could also speed up your website by combining some of those rules. For example, the forced www and https.

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.