2

I am trying to remove the php extension as well as hiding the sub directories using .htaccess file in my domain. However, I do not have much knowledge regarding regex and I am really stuck here. Would really appreciate if someone could assist with this!

What I'm trying to achieve is:
www.example.com/index.php to www.example.com/index
www.example.com/assets/php/company.php to www.example.com/company

CURRENT .htaccess FILE

Options +FollowSymLinks -MultiViews

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /
RewriteRule ^assets/php/(.*)$ /$1 [L,NC,R=301]

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

#To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

</IfModule>

Currently, I could successfully remove the .php extension from the URL as well as achieving http://www.example.com/company but it displays a 404 Not Found Error. I believe I'm missing a RewriteCond line, but I do not know how to write it. Or do I require another htaccess file in /assets/php/?

Will really appreciate if someone could assist with this! Nevertheless, thanks for reading.

Cheers,

TY

4
  • what do you mean by "hiding the sub directories" ? Commented May 13, 2015 at 20:41
  • Are all your files in /assets/php/ ? Or are there other subdirectories to worry about? You can write rules to prepend those directories under certain URLs but only if they follow some sort of pattern: stackoverflow.com/questions/5228901/… Commented May 13, 2015 at 20:57
  • @PedroLobito My apologies if I was unclear. The original URL was www.example.com/assets/php/company.php and I want to hide the assets & php folders. My desired outcome should be www.example.com/company Commented May 13, 2015 at 21:07
  • @ahoffner Besides my index.php which is at the public_html root folder, all the other php files are all within assets/php/. Do I need to have another htaccess file under /assets/php? Commented May 13, 2015 at 21:09

2 Answers 2

1

You can have your rules like this in root .htaccess:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /assets/php/([^.]+)\.php [NC]
RewriteRule ^ /%1 [L,NC,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/assets/php/$1\.php -f [NC]
RewriteRule ^([^.]+?)/?$ assets/php/$1.php [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^.]+?)/?$ $1.php [NC,L]
Sign up to request clarification or add additional context in comments.

7 Comments

Hi! Everything works fine after using your code, except the index page returns a not found. Any idea why?
The site is accessible at example.com but not when accessed through example.com/index.
Unless /index is a directory I believe example.com/index should also work by routing it to example.com/assets/php/index.php
actually, all the files in assets/php are working fine. Index is in the root folder at example.com/index.php. Is it possible to make it accessible through example.com/index too?
Thanks a million! You solved all the issues. I'll read up and refer to your solution to understand it. Cheers, have a nice day!
|
0

USe This

RewriteEngine on
RewriteBase /

#enforce www subdomain
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^sitename.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

#forward all requests, except new/admin, to the 'new' directory without the user's awareness
RewriteRule  new/admin  -  [S=2]
RewriteRule  ^$ new/    [L]
RewriteRule  (.*) new/$1 [L]

3 Comments

Thanks for your answer! However, after I included the code, the website returned a 500 Internal Server Error error. How should I resolve this?
In yor dirctory exist new/admin folder?
I'm still getting the error with the container folder new/admin created. Would really appreciate if you could explain to me your answer regarding creating a 'new' directory?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.