Url folder rewrite rule

I need help rewritting my URLs using htaccess my site structure looks like this

Root
-backoffice
-pages

  • static
    –images
    –css
    –js

And all my site pages are kept inside the folder called pages which can be acessed like this www.example.com/pages/contact.php

But i want to hide the folder so that my url will look www.example.com/contact.php

Note that i have other folders like backoffice and blog
Contents in backoffice should be accessed normally like this siteurl/backoffice/myaccount.php only the contents in pages folder should be affected

Hi @pandglobal and a warm welcome to the forum.

The expression used to modify the htaccess file and rewrite the URLs is “pretty urls”

Try searching this forum and read some of the Topics. Feel free to post any queries or problems.

Thanks @John_Betong but can you point me to one link that addresses my problem in the past so i can follow

Try these:

@John_Betong i followed the link and i tried something like this

#CHANGE SOME STATIC PAGES URL FOR THE PAGES FOLDER USING PRETTY URLS
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^pages/([a-zA-Z0-9-]+)(/?)$ /$1

Yet nothing works, all i wanted is not to have my contents in my pages folder accesed like this /pages/contact.php i want it to look as if the files where inside the root folder even though is kept in a subfolder called pages.

Even if i have to use pretty urls method to rewrite each of the pages one by one i dont mind since they are static files, someone should please point me to the right direction so that example.com/pages/contact.php will be accessed like this example.com/contact.php

The RewriteRule you have there does it the other way around, it says, "any request for /pages/whatever should go to /whatever)`.

But since you want it the other way around we should put this other way. But that poses a problem, because then everything under / will be served from /pages, and I don’t think that what you want.

So, what we could do instead is:

  1. If the URL exists in /, serve that
  2. If the URL doesn’t exist, see if it exists in /pages
  3. If it does exist in /pages, serve that

In code that would be

Options +FollowSymlinks -MultiViews
RewriteEngine On

# If the requested file does not exist in /
RewriteCond %{SCRIPT_FILENAME} !-f
# And the request is not for existing directory
RewriteCond %{SCRIPT_FILENAME} !-d
# And the requested file does exist in pages/
RewriteCond pages/%{SCRIPT_FILENAME} -f
# Then serve the requested file from pages/
RewriteRule ^(.*)$ /pages/$1

Alternatively you could actually move all files from /pages to / and then put redirects on the old URLs to new URLs. That’s easier configuration wise and better from a SEO point of view, as you’re actively indicating to search engines the files moved, instead of just serving them from some place else all of a sudden and leave it up to the search engines to draw their own conclusions from that. What if their conclusions are wrong and that has negative consequences on your rankings?

Thanks @rpkamp
I will try the code you sent and update you, i know moving all the files to the root saves us this headache but on the other hand i have read articles stating only some files should be kept in the root, even though i don’t know the security thats offers and they never told us how to handle issues like url, bcs it makes no sense having this vital pages called like this example. com/pages/contact.php

It didn’t work out, i tried it but it didnt work out, looks like am aiming at something that does not exist in Apache.

I think the place you said serve the requested file from pages supposed to be serve the requested pages from root,
That is if file found in pages folder then serve it from /file.php

Is there any code like that?

I would be tempted to adopt the modern practise of using the htaccess script to redirect everything to /index.php. Tests can then be tried and if the file exists in the pages directory then include the file and exit. If it is not in the pages directory and in another blog directory then include and exit… If it does not exist then show or include an error or search page.

@John_Betong this sounds highly technical to me but if it is a modern pratice as you pointed out then let me see the code and apply it,
But pointing everything to index.php how is that possible? Also won’t this affect folders like images, js, css or backoffice

Try this:

file: .htaccess

Options +FollowSymlinks -MultiViews
RewriteEngine On

# ONCE WORKING DISABLE <IfModule... and </ifModule>
# <IfModule mod_rewrite.c>
  # FORCE PRETTY-URLs  
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$  index.php/?$1 [L]
# </IfModule>

file: index.php

<?php DECLARE(STRICT_TYPES=1);
error_reporting(-1);

// DISABLE if ONLINE
// ini_set('display_errors','1');

  $page = $_SERVER['QUERY_STRING'] ?? 'home';
  $page = empty($page) ? 'home' : $page;
  $page = explode('/', $page)[0];


  if('home'===$page) :
    require 'pages/home.php';

  elseif( file_exists($page .'.php')) :
    require 'page/' .$page .'.php';

  else:
    # echo $page;

  endif;  

$aTests = [
  '',
  'home',
  'home.php',
  'index',
  'index.php',
  'page',
  'pages',
  'NON-EXISTENT-PAGE',
];

echo '<dl style="width: 42%; margin: 1em auto; line-height:2">';
echo '<dt> Test Pages </dt>';
  foreach($aTests as $key => $page) :
    echo '<dd> <a href="' .$page .'">' .$page .'</a> </dd>';
  endforeach;  
echo '<dl>';  
  
echo '<hr> <br>Title ==> ',
  $title = ucFirst($page) .' Page';

echo '<br>Page ==> ',
  $page;

Try the above and make sure every page is working OK:

Thanks we have all tried, but nothing seems to work, i think i should adopt a new strategy using file_get_contents(),
As this will be more better to use but i want know if there is a security issue with using it in an index.php file to call other pages?
If no security issues then is my safe bet than using .htacess rules that is not working

No, don’t do that. That’s not PHPs job.

Can you please elaborate on what part of the code I provided earlier doesn’t work?

Also, just because it won’t work the first time doesn’t mean Apache doesn’t support it. It certainly does, we just need to find instructions for it.

That’s what @John_Betong is aiming at, and that’s good thing to strife for, but first I’d clean up your URLs up by moving the files to the correct spot. Once you’ve done that you could as a second step rewrite everthing to only entry point. But as always, changing two things at once it not a good idea, because then when something breaks you won’t know which of the two changes caused it.

1 Like

Exactly my point, the rewrite is getting so confusing that i dont understand how my files structures are , if it works using some advanced method it may give me issues debugging it

@John_Betong code was first printing strict warning must be called first, which i did and then when i view some pages the codes are printed as text in front page

The supplied scripts were tested on my computer and worked fine.

More information is required rather than “it does not work”.

If the htaccess file was used and no changes then was the index.php file called?

Edit

If the message returned was “strict messages must be called first” then additional script has been added to the file, causing the error. Please use only the script supplied and make sure there are no hidden spaces before <?php

The index.php printed the codes, m
Since yours worked on your local machine, and the file structure is same with mine, then zip it and send to me as attachment so i can follow it up, i think thats best way.

Just two folders, and a page in the pages folder i will try it out. I fixed the issue of strict warning.

I am using a tablet and do not have access to the source files.

The supplied script was only to test to ensure pretty URLs worked and called the correct PHP files.

Try one file at a time, get that to be included, once working, move onto the next file.