TransWikia.com

How to remove folder name from URL path without interfering links like the homepage?

Stack Overflow Asked on February 25, 2021

Note: the domain is not online it’s made in localhost

Here’s a complex problem I am facing right now on my project is that I want to remove the folder name from the URL from

http://www.example.com/pages/contact-us

to

http://www.example.com/contact-us

Hence removing the /pages/ in this case –

This is my file structure-
File Structure

Now to access the files we have to access the pages then contact-us first so to remove this I have used this .htaccess code below –

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+pages/([^s]+) [NC]
RewriteRule ^ %1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^pages/)^(.*)$ /pages/$1 [L,NC]

When I use this the URLs can be accessed without using /pages/ but the homepage shows

403 Forbidden

So what should be the .htaccess code so to open the homepage without any errors and also please mention if this would kill my SEO and what’s the fix?

Please note if I need to tell you more, I have added these codes to the .htaccess file before adding the above code and it worked perfectly fine…

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html [NC,L]

Options -Indexes

php_value display_errors 1
AddHandler application/x-httpd-php .html .htm 

One Answer

It would be simpler if all pages (including the homepage) were served from the /pages/ subdirectory (which is what your code currently assumes).

(Aside: You didn't actually include the contents of your /pages subdirectory in your file structure, but if this subdirectory did not contain a directory-index document, eg. index.php or index.html (depending on the value of the DirectoryIndex directive) then you will indeed get a 403 Forbidden response. The same as requesting any directory without a directory-index document.)

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^pages/)^(.*)$ /pages/$1 [L,NC]

However, if your homepage is supposed to be served from the actual document root and not be rewritten to the /pages subdirectory (like all other URLs) then you will need to include an exception in the above rule block. For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/$
RewriteRule (?!^pages/)^(.*)$ /pages/$1 [L,NC]

To prevent requests for / (the document root) being internally rewritten to /pages/.

OR, make an exception for all directories (the document root is also a directory). For example:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^pages/)^(.*)$ /pages/$1 [L,NC]

OR, move your homepage (index.html) to the /pages subdirectory.

Correct answer by MrWhite on February 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP