htaccess Rewrite rule to send variable info to script but display clean url - .htaccess

What I'd like to do is when a URL like
http://localhost/sandbox/jcsearch/country/countryname
is typed by the user I would like to to point to
http://localhost/sandbox/jcsearch/index.php?country=countryname
but still retain the original clean URL in the address bar ie
http://localhost/sandbox/jcsearch/country/countryname
Is this possible? would it create any kind of redirect loop?

The rewrite would happen as follows:
RewriteEngine on
RewriteRule ^sandbox/jcsearch/([^/]+)/([^/]+)$ sandbox/jcsearch/index.php?$1=$2 [L,NC]
Since, the RewriteRule directive does not have the redirection flag (R) set, it will not change the URL in your browser's addressbar. So, by visiting
http://localhost/sandbox/jcsearch/country/countryname
user will get internally redirected to:
http://localhost/sandbox/jcsearch/index.php?country=countryname
Please note: You have to put the rewrite rules in the htaccess file in your server root directory.

Related

My htaccess passthrough rule redirects to the url instead

I'm trying to passthrough (not redirect!) an empty old page to its new location using an htaccess RewriteRule.
I essentially want the user to browse to mysite.com/page-old and to see that url in their browser but be delivered the content from mysite.com/page-new. The user should not be aware that the location changed.
RewriteEngine On
RewriteRule ^page-old/?$ /page-new [PT]
The actual result is that they are redirected to page-new instead.
I found the below on apache.org which seems to validate my code some, but this is giving me a 404 error.
Description:
Assume we have recently renamed the page foo.html to bar.html and now want to provide the old URL for backward compatibility. However, we want that users of the old URL even not recognize that the pages was renamed - that is, we don't want the address to change in their browser
https://httpd.apache.org/docs/trunk/rewrite/remapping.html
RewriteRule "^/foo\.html$" "/bar.html" [PT]
RewriteEngine On
RewriteRule ^example/my-stuff/$ /example/home/ [L,R=301]
check this answer as well
How to redirect a specific page using htaccess

.htaccess rewrite with subdirectory

I want to rewrite (not redirect) all url's of my site to a sub-directory of my site. For example:
http://example.com/example
would load the following:
http://example.com/public/example
Though, requesting http://example.com/public should not load the contents from public/public but from /.
Answers I've found on SO either do the above with redirect (which I don't want) or doesn't account for the special case above.
EDIT: further clarification:
I want every request on my site to go load under the public folder, but without being visible to the visitor. So requesting http://example.com/index.php will load the file from http://example.com/public/index.php. The url in the browser remains unchanged for the user.
Try the following rule :
RewriteEngine on
RewriteRule ^((?!public).+)$ /public/$1 [NC,L]
This will rewrite all requests from root to /public dir.

How do I redirect a web URL using .htaccess

I want to redirect users who enter this website URL:
http://www.myWebsite.com/bananas
to:
http://www.myWebsite.com/fruits/bananas
I cant test it because I'm sending this to somebody.
I have these but I don't know for sure which one works:
RedirectMatch 301 http://www.myWebsite.com/bananas(.*) http://www.myWebsite.com/food/bananas $1
Options +FollowSymlinks
RewriteEngine on
rewriterule ^bananas(.*)$ http://www.myWebsite.com/food/bananas $1 [r=301,nc]
Please specify if you want to redirect or rewrite. The rules you are using serve different purposes and you used both in your example.
Redirect: Actually load a different site when entering the url (end up at url and content of /fruits/bananas)
Rewrite: Url stays the same but server provides rewritten content (url stays at /bananas, but show /fruits/bananas content)
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
Also it is not clear if you only want one single directory to be redirected or all files that are contained in that directory.
Checkout this as a guide: http://www.htaccessredirect.net/
I believe you are looking for
Redirect 301 /bananas http://www.myWebsite.com/fruits/bananas
The HTTP 301 stands for Moved Permanently.
I haven't tested it, though.

.htaccess redirect from subdirectory to another domains subdirectory accordingly

I am trying to make a redirect from my primary domain to an secondary domain, but only if the primary domain's request is to a sub directory.
The sub directory I want to redirect from is FTP, so if the user makes the following request:
http://www.site1.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
it would be transformed to
http://www.site2.com/FTP/free/50b694124bd63/SaMple+PicTure.PnG
but if the user makes a request that does not involve the FTP folder, the user will not be redirected. Like so:
http://www.site1.com or http://www.site1.com/somethingelse/
I am, however; a bit lost when it comes to making .htaccess files. What I have tried to do so far is:
# Redirect users
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^ftp(.*)$ http://site2.com/FTP/$1 [L,R=301]
</IfModule>
Any directions or samples would be great :)
No need to use the rewrite engine for simple redirects. I think you just want to use the Redirect directive:
Redirect /FTP http://www.site2.com/FTP
By default, this will result in a "temporary" redirect response (HTTP status 302). If you're sure the URL of the second site will never change, you can cause a "permanent" redirect response (HTTP status 301) by adding the permanent argument:
Redirect permanent /FTP http://www.site2.com/FTP
Also, note that the path of URLs is case-sensitive. If you want http://www.site1.com/ftp to also redirect, you will either need to add a rule with the lowercase path,
Redirect /ftp http://www.site2.com/FTP
or use mod_speling.

Simple and neat .htaccess redirect help required

This is a strange one...
A while back I managed to write a .htaccess redirect that worked so that the URL was read like: www.website.com/mt?page=index - and what the real URL of this page was www.website.com/PageParser.php?file=index.php
The problem has been that the FTP system of my webhost hides .htaccess files even though they are allowed and do operate - and so I have checked back on local copies I have of my .htaccess files and none of them have the code as to how this works - and I've forgotten how I did it!!
Essentially, I am using wildcards so that anything after mt?page= will actually be showing PageParser.php?file= but without having the PageParser.php showing within the URL (and this is the important bit, because the index.php on my site root is actually sent through PageParser.php first so that anything which shouldn't be there is wiped out before the end user sees it) - so how can .htaccess redirect/rewrite the URL so that any link to /mt?page= show the file located at /PageParser.php?file= without changing the URL the user sees?
RewriteEngine On
RewriteRule ^(.*)mt?page=(.*)$ $1PageParser.php?file=$2
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^page=([^&]+)
RewriteRule ^mt$ /PageParser.php?file=%1.php [NC,L]
This rule will rewrite (internal redirect) request for /mt?page=hello to /PageParser.php?file=hello.php without changing URL in browser.
Your source URL example (www.website.com/mt?page=index) has index while target URL (www.website.com/PageParser.php?file=index.php) has index.php. The above rule will add .php to the page name value, so if you request /mt?page=hello.php it will be rewritten to /PageParser.php?file=hello.php.php.
If there is a typo in your URL example and page value should be passed as is, then remove .php bit from rewrite rule.
The rule will work fine even if some other parameters are present (e.g. /mt?page=hello&name=Pinky) but those extra parameters will not be passed to rewritten URL. If needed -- add QSA flag to rewrite rule.
This rule is to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.
P.S.
Better write no explanation (I knew it/I did it before .. but now I forgot how I did it) than having these "excuses". While it may be 100% true, it just does not sound that great.

Resources