.htaccess rewrite path without changing visible URL - apache

I have a link which looks like that:
www.domain.com/file.php
I want to create .htaccess entry so that if someone goes to
www.domain.com/folder/name
they would be redirected to
www.domain.com/file.php
I've created .htaccess entry which works and it looks like this:
RewriteRule ^folder/name$ /file.php [L,R=301,L]
The only problem I have is that in browser address field user sees
www.domain.com/file.php
where as I want him to see
www.domain.com/folder/name
Is it possible to do this and if so, how do I do that?

You don't need the redirect, that's causing it to reload the page which causes the new URL to appear in the address field. You just want to tell the server what directory path is represented by the "friendly" URL. Try this:
RewriteRule ^folder/name$ /file.php [L]

Related

.htaccess RewriteRule based on anchors

I am trying to update a RewriteRule. Previously, the redirect looked like this:
https://mywebsite.com/docs/#/en/introduction → https://manual.mywebsite.com/#/en/introduction
I would like to use /docs/ for something else now but I would like to keep redirecting requests containing a forward slash after the # to the manual subdomain. This is what I would like to achieve:
The old redirects continue working as usual:
https://mywebsite.com/docs/#/en/introduction → https://manual.mywebsite.com/#/en/introduction
This would not get redirected since there is no forward slash following the #:
https://mywebsite.com/docs/#overview
Here is what I have:
The .htaccess file containing the following existing rule which redirects everything:
RewriteRule ^docs/(.*) https://manual.mywebsite.com/$1 [L,R=301]
I tried this but it did not work (I tried with https://htaccess.madewithlove.com/ which says that my rule does not match the URL I entered):
RewriteRule ^docs/#/(.*) https://manual.mywebsite.com/#/$1 [L,R=301]
I also read about the NE (no escape) flag (https://httpd.apache.org/docs/2.2/rewrite/advanced.html#redirectanchors) which did not help either.
I am also sure that the server is actually using the file.
To summarize, my problem is that I want to match a URL containing /docs/#/ and redirect it to a subdomain, keeping the /#/ and everything that follows it.
Anchors are not part of the URL that's transmitted to the server: They stay within the browser, thus you can't build a rewrite rule that take anchors into account.
Inspect your access-logs to see what you get

URL rewriting using .htaccess not working

This is a link to a website I am making (I am very new to this) - http://www.inquivesta.iiserkol.ac.in. I am using .htaccess to try and rewrite the URLs for some of the pages. One of them is the about us page whose actual link is - www.inquivesta.iiserkol.ac.in/Home/index.php and I want it to look like www.inquivesta.iiserkol.ac.in/about/
So I wrote this in the .htaccess file :-
RewriteEngine On
RewriteRule ^about/?$ /Home/index.php [NC,L]
This doesn't seem to work. Whenever people open this page, I don't want it to display the filename. But the filename is stil being displayed. And if I try to open http://www.inquivesta.iiserkol.ac.in/about/ then it redirects to www.inquivesta.iiserkol.ac.in/Home/index.html

Add/prepend a GET variable name using redirects in htaccess (to fix broken URL)

I have a php script that relies on a $_GET variable called "serial." Normally the URL would look like this:
http://www.example.com/script.php?serial=XXXXXX
However, the person making the QR codes thought that the URL was too long and has already finished making QR codes that point to this URL:
http://www.example.com/script.php?XXXXXX
The part they left out was the "serial=", so PHP can't $_GET the variable anymore.
Is it possible to redirect using .htaccess to add the "serial=" back in?
Any help would be appreciated.
Thanks!
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^serial=
RewriteRule ^script\.php$ /script.php?serial=%{QUERY_STRING} [L]
If you want to redirect the browser, add a R to the inside of the square brackets so that it looks like [L,R].

.htaccess Rewrite without changing URL

I have a site that's coded mainly in PHP, but I'm trying to rewrite my dynamic php URL's into static HTML URL's.
But I want the address bar to still remain as the static HTML link.
I'm trying to accomplish this through .htaccess (I have no access to httpd.conf as I'm hosted on a shared account). Here is what's written in my .httaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^inventory-search-([^.]+)-by-([^.]+).html$ http://www.pianostudiosandshowcase.com/inventory.php?search=$1&by=$2 [R]
But I can't get the address bar to remain as the static HTML link.
Here is a link to show you what I mean:
http://www.pianostudiosandshowcase.com/inventory.php?search=manufacturer&by=1
What am I missing?
You need to remove both the R flag in your rewrite rule as well as the protocol/domain name:
RewriteRule ^inventory-search-([^.]+)-by-([^.]+).html$ /inventory.php?search=$1&by=$2 [L]
Both will cause the server to externally redirect the browser, telling it "what you were looking for is not at that URL, you need to go to this entirely different URL". The forces the browser to display the new location in its address bar.
If you internally rewrite it, the browser has no idea the URI that it sent as a request had been changed, therefore the address bar remains unchanged.

Need to create a RewriteRule in .htaccess for a Magento website

I am currently trying to put a RewriteRule into my .htaccess file for my Magento website that will allow me to write a category URL in the following way:
http://mydomain.com/dir/<direction>/order/<order>/<Magento category URL path>.html
What I am basically looking to do is use my robots.txt file to make some of the category URLs not appear (specifically when you apply a different sort order to the category).
So let's assume I have the following URL:
http://mydomain.com/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
I would like that to be rendered just as if it the URL was:
http://mydomain.com/footwear/mens-work-boots/motorcycle-boots.html?dir=asc&order=sales_index
The code I have put in my .htaccess file is as follows:
RewriteRule ^dir/(.*?)/order/(.*?)/(.*?)$ $3.html?dir=$1&order=$2
For some reason, when I have this in there, I get a 404 error. Can someone point me in the right direction to make this work for me?
I tried with this on my server
RewriteRule ^(.*)/dir/(.*?)/order/(.*?)/(.*?)$ $4.html?dir=$2&order=$3 [R,L]
and when i issue request
http://myserver/dir/asc/order/sales_index/footwear/mens-work-boots/motorcycle-boots.html
i get proper redirection to
http://yuave.dev:81/footwear/mens-work-boots/motorcycle-boots.html.html?dir=asc&order=sales_index
May be you are missing [L] flag on your request.