remove 3 directories in url via htaccess file - php

I want to convert the URL http://localhost/project1/mvc/public/content/1 to the URL http://localhost/content/1.
I have my .htaccess file placed in the public folder that removes the index.php in the URL.
Options -MultiViews
RewriteEngine On
RewriteBase /project1/mvc/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Here's my directory structure:
Project 1
---mvc
-----app
-----public
I need help in the creating .htaccess file for this. What is the syntax for modifying the URL and which folder/s should I put the .htaccess file/s?
Thanks

You can use this code in your DOCUMENT_ROOT/.htaccess file (above a directory level of mvc):
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!/project1/mvc/public/).*) project1/mvc/public/$1 [L,NC]

Related

htaccess - I change rewritebase but still needs public folder in url

My index.php is in public folder and in my .htaccess file I say that rewrite base is the public directory. But when I open the cms.test, it gives me cms folders.. for the use index php, still I need to go public folder..
here is my .htaccess file
Options -MultiViews
RewriteEngine On
RewriteBase /var/www/cms/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA]
How do I fix this ?
Set your public folder to be the root directory
Options -MultiViews
RewriteEngine On
RewriteBase /var/www/cms/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA]
Set your public folder to be the root directory

Rewrite Specific PHP file to directory with htaccess

So I have a URL that is domain.com/s_cart.php?&pid=1 and /s_cart.php?&gid=2
How would i go about setting a .htaccess to rewrite s_cart.php to the following
domain.com/order/&pid=1
Tried the following
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/order([/]*)$ ./s_cart.php?gid=5 [L,NC]
Didn't work.
Basically I'm trying to add domain.com/order?pid=1
When the actual url currently is domain.com/s_cart.php?pid=1

.htaccess Hide php param keys from url but dont change physical path

I am looking for a solution to hide param keys from my url
for example /page1.php?city=Lahore
I want it to rewrite as /page1/Lahore
but the most important thing is Lahore is not a directory exist on server I want it to point to same file page1.php just rewrite url externally
thanks
create an .htaccess file from the root and paste this code
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule ^page1/(.*)? /page1.php?city=$1 [L]
or you could also put it in your website apache conf file.
try this code
for .htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /projectfoldername/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^page1/(.+?)/?$ /projectfoldername/page1.php?key=$1 [L,QSA]
page1.php code will be
<?php
echo $_REQUEST['key'];
?>
then call
http://localhost/projectfoldername/page1/1947
output will be :1947

Htaccess code redirects to XAMPP index page

I am trying to write htaccess codes which redirect all URLs apart from directly linked files (e.g images & css files) to my index.php file for processing
http://localhost/testsite/login
redirects to
http://localhost/testsite/index.php?cmd=login
But the htaccess code instead redirects me to the XAMPP homepage. This is my htaccess code
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?cmd=$1 [QSA,NC,L]
What am I doing wrong?
Remove / from target and use proper RewriteBase:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /testsite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?cmd=$1 [QSA,L]
/index.php will route it to site root index.php instead of index.php in current directory.

can another .htaccess file can work in subfolder

I have a codeigniter site http://piyukarts.in/mss/ where in .htaccess file under mss contains
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
# Send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
but i cant access a link http://piyukarts.in/mss/posts, but i can access piyukarts.in/mss/index.php/posts, with this index.php/ after mss/
piyukarts.in/ is wordpress site, also having a .htaccess file
Please help.. thanks a ton in advance...
I made this change in .htaccess file and its working fine now.
RewriteEngine On
RewriteBase /mss
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|css|docs|js|system)
RewriteRule ^(.*)$ /mss/index.php?/$1 [L]
To answer:
can another .htaccess file can work in subfolder
is:
Yes.
.htaccess files in any folder in the file path tree to the file, will be applied to the file.
so file as /root/home/site/includes/css/horses.css the htaccess in any of those folders would be applied to the file horses.css .

Categories