Htaccess redirect url path different path - php

I want to masking url with htaccess. I tried a few method but didn't work.
I used this code
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
RewriteRule ^ - [L]
RewriteRule ^ index.php
RewriteCond %{HTTP_HOST} ^em.example.com/lib/view/map$ [NC]
RewriteRule ^pathurl/(.*)$ /em.example.com/map/$1 [R=301,NC,L]
Url is
https://em.example.com/lib/view/map.php
I want to redirect to
https://em.example.com/map

Try
RewriteRule ^/lib/view/(.*)$ $1 [NC]
RewriteRule ^(.*).php$ $1 [R=301,NC,L]
First rule removes lib/view/ , Second rule removes .php

Related

htaccess redirect not exist file and show link like exist

This is my htaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^https://example.com%{REQUEST_URI} [NS,R,L]
RewriteRule ^((?:[a-zA-Z0-9_-]|%20)+)/?$ member.php?id=$1
With this htaccess, i can remove .php and i can show my "example.com/member.php?id=1" page as "example.com/3".
Now i want to show "example.com/product.php?id=1" as "example.com/product/1" but the interesting part, there is not exist "product" file. Is it possible to show as "example.com/product/1"?
Thanks
Have it like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^ https://example.com%{REQUEST_URI} [NE,R=301,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# product rewrite
RewriteRule ^product/([^/]+)/?$ product.php?id=$1 [L,QSA,NC]
# member rewrite
RewriteRule ^([^/]+)/?$ member.php?id=$1 [L,QSA]

.htaccess code making a wrong url redirect

This is my .htaccess code
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^hashstar.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.hashstar.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php timer/index.php
My root directory is set to folder app
In app folder, I have 2 folders
1.) Manage
2.) Timer
To access manage folder I have to type in URL
http://www.example.com/manage/
To access timer folder I have to type in URL
http://www.example.com/timer/
if I use these Url by adding a slash in URL (/)
the URL loads perfectly
but if the don't add the (/) in URL then it makes a redirection through the root directory
URL should look like => http://www.example.com/manage
It becomes like => http://www.example.com/app/manage/
What's the reason behind it & how to fix this problem?
Any helps appreciated. Thanks in advance.
Reason of this redirect is that rewritten URI is a real directory without trailing slash. When this happens, Apache's mod_dir module acts on it by redirecting to a URI with a trailing slash.
To prevent use this .htaccess:
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?hashstar.com$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
At a glance (I didn't try to reproduce these rules in my Apache2 server), it would seem that this is the culprit:
RewriteRule (.*) /app/$1 [L]
the Regular Expression (.*) will catch Any character greedily. Your Rewrite Rule is simply adding /app/ between the domain and the script.
Find and group <all non-null characters>,
and transform it to /app/<all non-null characters>
The URL www.example.com/myscript.php would be rewritten as www.example.com/app/myscript.php
EDIT
so what code edit do u suggest? #SsJVasto
You could simply exclude / from the greedy rule:
RewriteRule (.*)/ $1 [L]
This way, you are storing the result of anything ending with a /, but without said / into $1. Since the / isn't inside the group, it will be forgotten.
If you want to handle repetition (like http://www.example.com/myscript.php//), you can add a + sign to handle one or more occurrences:
RewriteRule (.*)/+ $1 [L]

How to define base URL in .htaccess

I am redirecting
https://www.example.com/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/<a number>/<the-page-title>
Now I want to move the website to a sub folder so the redirection will be
https://www,example.com/folder/inter.php?pid=<a number>&title=<the page title>
to
https://www.example.com/folder/<a number>/<the-page-title>
I am using the below .htaccess code
RewriteEngine On
RewriteCond %{THE_REQUEST} /inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,NE,L]
RewriteRule ^(\S+)\s+(.*)$ $1-$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^(\S+)$ /$1 [NE,R=302,L]
RewriteRule ^([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please Suggest.
thanks!
If you are using .htaccess in root directory you can add /foldername
RewriteCond %{THE_REQUEST} /folder/inter\.php\?pid=([^&\s]+)&title=([^\s&]+) [NC]
RewriteRule ^ /foldername/%1/%2? [L,R]
To both rules
RewriteRule ^folder/([^/]+)/([^/]+)/?$ inter.php?pid=$1&title=$2 [L,QSA]
Please try above rules it I didn't tried it for now.

how write a rule to Hide url parameter using htaccess

i am trying to rewrite URL using htaccess.
i need
1> temp.example.com/save.php?item=xxxxx&id=xxx&type=1
2> temp.example.com/save.php?item=xxxxx&id=xxx&type=2
to be like
1> temp.example.com/save/xxxxx/xxx
2> temp.example.com/save/xxxxx/xxx
have to hide last parameter .
my htaccess has
RewriteCond %{THE_REQUEST} \s/save\.php\?item=([_0-9a-zA-Z-]+)\s&id=([_0-9a- zA-Z-]+)\s&type=([_0-9a-zA-Z-]+)\s [NC]
RewriteRule ^temp/save/%1/%2/%3? [R=301,L]
RewriteRule ^temp/save/([_0-9a-zA-Z-]+)$/([_0-9a-zA-Z-]+)$/([_0-9a-zA-Z-]+)$ /save.php?item=$1&id=$2&type=$3 [L]
Thanks
You can use these rules in root .htaccess:
RewriteCond %{THE_REQUEST} /save\.php\?item=([^\s&]+)&id=([^\s&]+)&type=([^\s&]+)\s [NC]
RewriteRule ^ save/%1/%2/%3? [R=302,L]
RewriteRule ^save/([\w-]+)/([\w-]+)/([\w-]+)/?$ save.php?item=$1&id=$2&type=$3 [L,QSA,NC]
RewriteEngine On
RewriteRule ^save.php/([a-zA-Z0-9_-]+)/(.)/(.)$ /save.php?id=$1&order_id=$2&page_no=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
now you can access three parameter by pass like
temp.example.com/save/par1/par2

htaccess remove www and redirect to accessed url

I am using codeigniter framework i need to force to remove www from the url so I am using this code
RewriteEngine on
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,QSA,NC,L]
This code is forcing removal of www. but the problem is when a user access a link with www
eg:www.mydomain.com/framework/article/sometestarticle368/
It is redirecting to
www.mydomain.com/framework/
How can i fix this ?
Change the order of your rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI}/$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|css|images|js|plugins|scripts|fancybox|uploads|mobile|robots\.txt)
RewriteRule ^(.*)$ /framework/index.php?/$1 [L,QSA]
Otherwise your 2nd rules runs first and change the URI to /framework/... before the www removal rule..
Try this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]
Thanks

Categories