Use .htaccess To Redirect Page Without Changing URL With 'mod_rewrite' - apache

I have a script on my page that checks the url for the word signing. If the word exists, it shows the signin form in a dropdown on page load. I'd like to avoid creating extra files on the server, so I was wondering how I could do this:
When the user is at either of these urls:
http://abramobile.com/signin
http://abramobile.com/sign-in
Then direct the page back to the homepage, but leave 'signin' or 'sign-in' in the URL so that the script on my page detects that it's in the URL.
What would the mod_rewrite be to put in my .htaccess file? Thanks!

This should do it. This matches against the URI being exactly /signin or /sign-in and internally rewrite the URI to / so that that gets served.
RewriteRule ^sign-?in$ / [L]

Related

Apache rewrite url and hide old url

I want to simplfy an URL but cant find how to do that.
Here is the original URL that exists and works.
https://example.com/s/732kglm
I want to use this different URL for the same content.
https://example.com/info
I can do that in apache with a redirect
Redirect permanent /info /s/732kglm
Now if I open https://example.com/info I will be redirected to https://example.com/s/732kglm but the browser shows the original URL.
I want to hide tho original URL so that user only sees the simplified URL https://example.com/info
How can I achieve this?
It sounds like you need an internal redirect. I don't have access to apache to test, but something like the following should work.
RewriteEngine on
RewriteRule "^/info$" "/s/732kglm"

I wanna stop direct access to and folder using htaccess

I wanna set up an download page.
I need to access those pages only through a login page and stop all the direct access to the download page path and also its files and again if anyone tries to access those pages directly then it should automatically redirect to login page itself or else at least with a 404 error.
How can I do all these in .htaccess file. It will be very much helpful in developing my website.
Thanks in advance
Assuming that your all of your downloads are inside of a downloads directory, something like this should do the trick.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^https?://(?:www\.)?your.site.*
RewriteRule ^downloads\/?.* http://your.site/login [R=302,L]
Alternatively (and ideally) you should instead keep the pages outside of the site's root directory and download them through a proxy page.

Htaccess one and two parameters

I'm a newbie in htaccess and I'm having a problem. See my htaccess code below.
RewriteEngine On
RewriteRule ^([^/]*)/lesson/([^/]*)\.htm$ /thesis/index.php?page=$1&lesson=$2 [L]
RewriteRule ^([^/]*)\.htm$ /Thesis/index.php?page=$1 [L]
When the user click or navigate to a link which is having two parameters. The page is giving the requested page correctly. The URL looks like this http://www.example.com/home/lesson/1.1.htm
But after that, when the user click the link which is having one parameter for example is the logout script. everything goes wrong. the URL became http://www.example.com/home/lesson/logout.htm
I hope someone could help me.
Looks like you have relative URLs and because of the /'s in the URL, you're base is messed up. You should either change all your links to absolute URLs, or include the base URI in the header of your pages:
<base href="/" />
Because the URL you're accessing is: http://www.example.com/home/lesson/1.1.htm, and as far as the browser knows, the "page" is 1.1.htm and the "path" is /home/lesson/. Thus, if the browser sees a relative URL (something that doesn't start with an "http" or "/"), it needs to append a base to the beginning in order to properly resolve it. Since the browser thinks the path is /home/lesson/, it attaches that to the beginning.

Multiple Domains to Display Content from Landing Pages on Another Domain

We have created a bunch of landing pages on a Joomla CMS system, such that the URL for each landing page is www.domain.com/page1.html and www.domain.com/page2.html, and so on. Of course the page1.html isn't really an HTML file it is a dynamic CMS page, just rewritten with htaccess.
The goal is to have one of our other domains, something like www.uniquedomain1.com show the content of www.domain.com/page1.html. Or, another domain like www.uniquedomain2.html show the content of www.domain.com/page2.html.
This needs to be search engine friendly so we can't use URL masking. Also we can't use HTACCESS redirects as this actually changes the URL in the browser bar. Need to keep the www.uniquedomain1.com URL in the browser bar.
Tried Apache VirtualHost options without any luck. You can park in a directory but not from a URL.
Ended up parking the domains on one folder, and then creating a PHP script to detect the domain host and then use CURL to query the correct url and deliver content. This whole thing seems ridiculously over complicated, and of course CURL isn't the best option, but it is all we could get to work.
Any thoughts on how to do this, or a better solution?
You can use HTACCESS redirect rules to do it without performing a redirect.
Change the html file names to be the domain name of the desired domain like domain.tld and do something like this in an .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9\.-]+\.[a-z]+) [NC]
RewriteRule ^$ /%1.html [L]
A quick test of this worked for two of my test (sub)domains test.domain.tld and test2.domain.tld. Both properly redirected to files with the names test.domain.tld.html and test2.domain.tld.html without modifying the URL.
You could also just use your PHP wrapper script to grab the content of each of the miscellaneous html files and output them.
If you renamed all of your HTML files (as in my previous suggested answer) to be domain.tld.html you could do it fairly easily. Something might look like:
<?php
require($_SERVER['SERVER_NAME'] .'.html');

Understanding difference between redirect and rewrite .htaccess

I'd like to understand the difference between redirecting and rewriting a URL using .htaccess.
So here's an example: Say I have a link like www.abc.com/ index.php?page=product_types&cat=88 (call this the "original" url)
But when the user types in abc.com/shoes (let's call this the "desired" url), they need to see the contents of the above link. To accomplish this, I would do this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)shoes(.*)$ index.php?page=product_types&cat=88
Nothing wrong with this code and it does the trick. However, if I type in the original url in the address bar, the content comes up, but the url does not change. So it remains as www.abc.com/index.php?page=product_types&cat=88
But what if I wanted the desired url (/shoes) to show up in the address bar if I typed in www.abc.com/ index.php?page=product_types&cat=88? How would this be accomplished using .htaccess? Am I running into a potential loop?
Some of the explanation can be found here: https://stackoverflow.com/a/11711948/851273
The gist is that a rewrite happens solely on the server, the client (browser) is blind to it. The browser sends a request and gets content, it is none the wiser to what happened on the server in order to serve the request.
A redirect is a server response to a request, that tells the client (browser) to submit a new request. The browser asks for a url, this url is what's in the location bar, the server gets that request and responds with a redirect, the browser gets the response and loads the URL in the server's response. The URL in the location bar is now the new URL and the browser sends a request for the new URL.
Simply rewriting internally on the server does absolutely nothing to URLs in the wild. If google or reddit or whatever site has a link to www.abc.com/index.php?page=product_types&cat=88, your internal server rewrite rule does absolutely nothing to that, nor to anyone who clicks on that link, or any client that happens to request that URL for any reason whatsoever. All the rewrite rule does is internally change something that contains shoes to /index.php?page=product_types&cat=88 within the server.
If you want make it so a request is made for the index.php page with all of the query strings, you can tell the client (browser) to redirect to the nicer looking URL. You need to be careful because rewrite rules loop and your redirect will be internally rewritten which will cause a redirect which will be internally rewritten, etc.. causing a loop and will throw a 500 Server Error. So you can match specifically to the request itself:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?page=product_types&cat=88
RewriteRule ^/?index.php$ /shoes [L,R=301]
This should only be used to make it so links in the wild get pointed to the right place. You must ensure that your content is generating the correct links. That means everything on your site is using the /shoes link instead of the /index.php?page=product_types&cat=88 link.