skip to Main Content

i’m using .htaccess to create clean urls.
here is a piece of .htaccess file:

Options +FollowSymLinks -MultiViews
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteCond %{REQUEST_URI} !(/$|.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L] 

RewriteRule ^posts/page/([0-9]+)/$ posts.php?page=$1
RewriteRule ^posts posts.php

it works fine but problem is both urls work!
is there a way to only make site.com/posts/ work and return ‘not found’ on site.com/posts.php(or redirect to site.com/posts/)

think i read somewhere two urls for the same page is bad for seo.

3

Answers


  1. You can create a rewrite rule that returns 404 HTTP code on every “.php” URL :

    RewriteRule .*.php$ - [R=404]
    
    Login or Signup to reply.
  2. This is working fine for me. Hope it will work fine for you as well.

    RewriteEngine On
    Options -Multiviews
    
    #For rewriting request to posts.php
    RewriteCond %{REQUEST_URI} ^/posts/?$
    RewriteRule .* /posts.php [L,END]
    
    #For returning 404 on directly accessing /posts.php
    RewriteCond %{REQUEST_URI} ^/posts.php$
    RewriteRule .* - [R=404,L]
    
    Login or Signup to reply.
  3. To redirect /posts.php to /posts you can use this

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} /posts.php [NC]
    RewriteRule ^ /posts [NE,L,R]
    #Your other rules
    

    If you want to redirect the orignal request to 404 , change the RewriteRule line to this

    RewriteRule ^ - [R=404,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search