0

I want to change the URL from one to another. Exactly from this:

https://example.com/pages/page.php

to this:

https://example.com/page.php

Physically page.php is in the /pages folder and I want to left it like that. I want to change the visible URL only.

I've tried to use rewrite with htaccess like this: RewriteRule ^pages/(.*)$ /$1 [R=301,NC] but it returns 404.

Update

I've tried this:

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

and now I see page.php from both https://example.com/pages/page.php and https://example.com/page.php without 404.

BUT it's still not exactly what I expected. I want after going here https://example.com/pages/page.php to see: https://example.com/page.php

RustBeard
  • 81
  • 10
  • Have you looked at: https://stackoverflow.com/questions/18973058/how-to-remove-folder-name-from-url-using-htaccess – SH_ May 09 '20 at 11:38
  • 1
    Does this answer your question? [how to remove folder name from url using htaccess](https://stackoverflow.com/questions/18973058/how-to-remove-folder-name-from-url-using-htaccess) – CoderCharmander May 09 '20 at 11:45
  • Yes, I saw it and tried. This is the same, I have 404 ;) Autor of this post have the same problem, you can see in comments. – RustBeard May 09 '20 at 11:51
  • You can access both URLs? If so what happens if you redirect URL by adding this line at bottom of htaccess: `Redirect /pages/page.php /page.php` – Cyborg May 10 '20 at 01:41

1 Answers1

-3

ModRewrite would be a more decent solution I think, but in PHP you could just use include/require

# /page.php
require_once('./pages/page.php');
clash
  • 427
  • 4
  • 11
  • 1
    The OP says: **Physically page.php is in the /pages folder and I want to left it like that** – Cyborg May 09 '20 at 11:56
  • True, my answer is not valid here, but if OP didn't want an PHP valid solution, there shouldn't be a PHP tag. – clash May 09 '20 at 13:39
  • OP uses PHP tag cause he is using Apache with PHP. He want redirect/rewrite or some other function for PHP. `.htaccess` I use for PHP only not sure about you. – Cyborg May 09 '20 at 15:08
  • "or some other function for PHP" - And I gave him an answer for PHP, which OP doesn't want. That is ok. Still, Apache and rewrite has little to nothing to with php per se. – clash May 09 '20 at 20:49