Remove base folder from url with htaccess - .htaccess

I'm trying to rewrite
http://127.0.0.1:8888/folder/page/display-6.html
to
http://127.0.0.1:8888/page/display-6.html
the htaccess file is in directory
this is my .htaccess file :
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_URI} folder/(.*)
RewriteRule ^(.*)$ / [L]

You can use this rule:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} /folder/(\S+)
RewriteRule ^ /%1 [L,R]

Related

Need help writing htaccess rule

Currently my localhost URL is like localhost/apnaujjain/page.php?page_id=1&action=contacts
Now I want to write a rule so that when I go to above url it just rewrite the url like localhost/apnaujjain/contacts.html
See it should convert .php into .html without affect page content.
Until now I've tried
Options +FollowSymLinks -MultiViews
RewriteEngine on
#RewriteBase /
RewriteCond %{THE_REQUEST} (.*)\.php
RewriteRule ^(.*)\.php $1.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.php?page_id=1$&action=2$
RewriteRule ^(.*)\.php $2.html [R=301,L]
RewriteCond %{THE_REQUEST} (.*)\.html
RewriteRule ^(.*)\.html $1.php [L]
But it's not working.
You can use this code in /apnaujjain/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /apnaujjain/
# redirect localhost/apnaujjain/page.php?page_id=1&action=contacts to
# localhost/apnaujjain/1/contacts.html
RewriteCond %{THE_REQUEST} \s/+.+?\.php\?page_id=([^\s&]+)&action=([^\s&]+) [NC]
RewriteRule . %1/%2.html? [R=301,L]
RewriteRule ^([^/]+)/([^.]+)\.html$ page.php?page_id=$1&action=$2 [NC,L,QSA]

.htaccess : subdomain not mapping correctly

I am trying to make this work:
forum.domain.com -> domain.com/forum
Problem is that this works, but then when I try to access topic of a forum, it says 404.
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^forum\.domain\.com\$1$
RewriteCond %{HTTP_HOST} ^www\.forum\.domain\.com\$1$
RewriteCond %{REQUEST_URI} !^http://forum.domain.com/
RewriteRule ^(.*)$ /forum/index.php [R=301,P]
</IfModule>
There are some syntax error and HTTP_HOST only matches domain name without URI.
Replace your code with (assumin mod_proxy is enabled):
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?forum\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/forum/ [NC]
RewriteRule ^ /forum/index.php/$1 [P,L]

Redirect query string to non-query string?

I am looking for an .htaccess rule, which redirects:
http://abcde.com/?q=abc
Where abc can be any string to:
http://abcde.com/
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^$ http://%{HTTP_HOST}/ [R=301,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^q=.+ [NC]
RewriteRule ^$ /? [R=301,L]

rewrite rule for shop in lithium / apache

I have a lithium installation and all the .htaccess works fine.
I need to install OpenCart as a shopping cart in app/webroot/shop
I copied all the files and also changed the .htaccess file in the root folder of lithium installation as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule shop/(.*) /app/webroot/shop/$1 [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Still when I browse http://domain.com/shop it takes me to http://domain.com/app/webroot/shop/
With an error on page:
Exception
lithium\action\DispatchException (code 404)
Action `webroot` not found.
Please help me in solving this problem.
You may try this instead:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/app/webroot/shop/? [NC]
RewriteRule ^shop/(.*) /app/webroot/shop/$1 [L,NC]
RewriteCond %{REQUEST_URI} !/app/webroot/? [NC]
RewriteRule ^$ /app/webroot/ [L,NC]
RewriteCond %{REQUEST_URI} !/app/webroot/? [NC]
RewriteRule ^(.*) /app/webroot/$1 [L,NC]
</IfModule>
The problem is that your last "catch all" rule is also redirecting all requests for the shop to lithium, which you don't want.
Try this
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Rewrite all URLs that start with /shop to /app/webroot/shop
RewriteRule ^shop/.? /app/webroot/shop%{REQUEST_URI} [L]
# Rewrite all URLs that don't start with /app/webroot/shop to /app/webroot
RewriteCond %{REQUEST_URI} !^/app/webroot/shop
RewriteRule .? /app/webroot%{REQUEST_URI} [L]

htaccess for subfolders

I'm new htaccess. So i need to rewrite .php to .html in url.
my files located in
http://www.domain.us/sub1/sub2/sub2/index.php
I need to rewrite as
http://www.domain.us/sub1/sub2/sub2/index.html.
I tried something
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} site
RewriteRule ^(.*)\.php$ http://www.domain.us/sub1/sub2/sub3/$1.html [R,L]
But It shows 404 error.
Thanks.
Put this code in your DOCUMENT_ROOT's .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s/*sub1/sub2/sub2/.*\.php [NC]
RewriteRule ^(.*)\.php$ /$1.html [NC,NE,R=301,L]

Resources