Reputation: 39
I have a project with the name of social app which contains has a structure like this:
currently if I want to vists any page like profile.php I have to visit localhost/social-app/pages/login.php but I want to modify it and change the url to localhost/social-app/login.php. basically I want to get rid of the pages and make my URL a bit cleaner
Upvotes: 1
Views: 44
Reputation: 41219
In your htaccess
in the root , put the following :
RewriteEngine on
RewriteRule ^social-app/login\.php$ /social-app/pages/login.php [L]
Now instead of going to long URL you can just type /social-app/login.php
to access the file in pages
folder.
EDIT :
To remove the directory name for all files , you can use the following :
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/social-app/pages/$1.php -f
RewriteRule ^social-app/([^.]+)\.php$ /social-app/pages/$1.php [L]
If the rule above fails , then use this :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^social-app/(.+)\.php$ /social-app/pages/$1.php [L]
This will internally map a request for /social-app/filename.php
to /social-app/pages/filename.php
.
Upvotes: 3
Reputation: 45914
If everything (ie. images, CSS, JS and PHP pages) need to be rewritten to the /pages
subdirectory then you can do something like the following in the /social-app/.htaccess
file using mod_rewrite:
RewriteEngine On
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
This unconditionally rewrites everything that is not already prefixed with pages/
to the pages
subdirectory. eg. /social-app/login.php
is internally rewritten to /social-app/pages/login.php
.
If you have static resources in other locations that should not be rewritten then include a filesystem check to prevent requests that already map to existing files from being rewritten. For example:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^pages/ pages%{REQUEST_URI} [L]
If you are changing an existing URL structure then you'll need to redirect the old URLs that are indexed by search engines and perhaps linked to from third parties. For example, the following "redirect" would need to go before the above rewrite:
RewriteBase /social-app
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^page/(.*) $1 [R=301,L]
Upvotes: 2