skip to Main Content

In order to have sanitized URLs in my domain for SEO purposes, I need to make some adjustments to the .htaccess file but I need some help with it.

I have the next files:

  • ./index.php
  • ./work.php
  • ./project-page.php

And the URL’s should be:

  • http://example.com/
  • http://example.com/work
  • http://example.com/work/first-project (project title taken from
    project id)

First question: Do I need to get the ./project-page.php file inside a folder named work or is it redirected from .htaccess file and how? If the answer is the second, I must replace URLs inside that file such as images and includes, right?

Second question: But what if the user gets to http://example.com/work/? I get an error that that page does not exist, and I need that to work too. I don’t know what’s the best practice in this case.

Thanks for your answers!


EDIT:

I already have this config on my .htaccess

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteBase /

  #### PERSISTENT CONTENT ####

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

  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule ^(.*)$ $1.php


  RewriteRule ^work-(.*)$ project-page.php?id=$1
</IfModule>

So right now, I can access to example.com/work as ./work.php and example.com/work-first-project as ./project-page.php?id=1 BUT I want it to be example.com/work/first-project

So, is it better to move ./project-page.php inside a new folder named /work/ or it’s not needed. And how can I get it to work as example.com/work/first-project with .htaccess or any other methods.

2

Answers


  1. Chosen as BEST ANSWER

    Cannot get it to work. As Sean suggested, I put the removing trailing slash at the top before the others sentences. Now the thing disrupting the whole document is the last sencence. Which was originally put to remove the file extensions. If I delete that thing, the others work ok.

    <IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteRule ^(.*)/$ /$1 [R=301,L]
      RewriteBase /
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME}.php -f
      RewriteRule ^work/(.*)$ project-page.php?id=$1 [R=301,L]
      RewriteRule ^(.*)$ $1.php
    </IfModule>
    

  2. You want to use RewriteRules, for example these will work for the first two examples:

    RewriteRule "^/$" "/index.php"
    RewriteRule "^/work$" "/work.php"
    

    https://httpd.apache.org/docs/2.4/rewrite/intro.html#rewriterule

    You can structure your folders however you like so long as the second param points to a valid file.

    You can put one RewriteRule before the rest in .htaccess to remove the trailing slash

    RewriteRule "^(.*)/$" "/$1"
    

    You can also use an HTTP301 (permanent) redirect with R=301, and ensure no further rules are processed once the redirect with L.

    RewriteRule ^(.*)/$ /$1 [R=301,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search