Mohammad Badzohre
Mohammad Badzohre

Reputation: 21

php mvc – Remove Public from URL

I know this is a very popular question but I haven't been able to find a working solution in my project I want to set my document root to the public folder

change url for example from /public/login to /login

.htaccess code

  RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?$1 [L,QSA]

Upvotes: 1

Views: 394

Answers (2)

arkascha
arkascha

Reputation: 42984

I'd say this is the immediate answer to the question you asked:

RewriteEngine On
RewriteRule ^/?public/(.*)$ /%1 [QSA,R=301,END]
RewriteRule ^ /public%{REQUEST_URI} [QSA,END]

But I assume this is more what you actually need, your question is vague in that:

RewriteEngine On
RewriteRule ^/?public/(.*)$ /%1 [QSA,R=301,END]
RewriteRule ^ /public/index.php?%{REQUEST_URI} [QSA,END]

Obviously the rewriting module needs to be loaded and enabled into the http server and the interpretation of distributed configuration files (".htaccess") has to be granted in the http server's host configuration if you really want to use such.

It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Upvotes: 1

Nawin
Nawin

Reputation: 105

Sometimes .htacess code doesnot work due to mod_rewrite. Try once using mod_rewrite enable. Alternately you can move you index.php file from public folder to main folder and update link that are used in index.php file. Another way is to use php artisan serve to run project and use htaccess that you have used.

Upvotes: 0

Related Questions