Hide actual URL and display alternate URL using htaccess - apache

I want to redirect user from localhost/test --> localhost/new/test/index.php
But user should see localhost/test in the address bar though the content is fetched from localhost/new/test
How to achieve this using htaccess. Any help is appreciated.
Existing solutions on hiding actual URL did not work for me. So kindly help to resolve this issue.
Example that we are trying
folder level 1 => localhost/test/index.php
folder level 2 => localhost/new/test/index.php
User types localhost/test/index.php in URL but he should be shown the content of localhost/new/test/index.php without displaying localhost/new/test/index.php
User should still see the old URL localhost/test/index.php

What you need is called internal or silent redirection without using R flag (use for external redirection).
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# if URI doesn't start with /new
RewriteCond %{REQUEST_URI} !^/new/ [NC]
# redirect to /new/$1
RewriteRule ^(.+)$ /new/$1 [L]
Suggest you to read: Apache mod_rewrite Introduction

Related

URL Rewriting Add a folder name to URLs

I have an apache2 server.
My directory structure is - (below is inside - /some-folder/abc)
- app
- otherfolder
- pqr.php
- xyz.php
- js
- css
.htaccess is placed inside /some-folder/abc
Context root for virtual host is set till - /some-folder/
Now, I want my users to enter this URL - http://someserver.com/abc/xyz or http://someserver.com/abc/pqr
I want to open the page at - http://someserver.com/abc/app/xyz.php or http://someserver.com/abc/app/pqr.php
How can I achieve this using URL Rewriting mod_rewrite
This is what I have so far, which is not working -
Options +FollowSymLinks -MultiViews
#Turn mod_rewrite on
RewriteEngine On
#RewriteBase /abc
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /app/$1\.php [L] # this doesn't work either
# RewriteRule ^(.*)$ app/$1.php [L] # this doesn't work either
# RewriteRule ^abc/(.*)$ abc/app/$1.php [L] # this doesn't work either
Thank you for your help.
If possible, I would also like to take all query params using forward slash
ex - http://someserver.com/abc/app/xyz.php/xId/123/uId/938/sdh/8374 instead of http://someserver.com/abc/app/xyz.php?xId=123?uId=938?sdh=8374
There is no pattern for query params, they can be anything for an page. Is this possible by a generic mod_rewrite, or do I have to rewrite urls for each page, and each time my developers add a new query param?
This rule should work for you from /abc/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/abc/app/$1.php -f
RewriteRule ^(?!internal)(.+?)/?$ app/$1.php [L,NC]
RewriteCond makes sure that we have a corresponding .php file in /abc/app/ sub-directory.
(?!internal) is negative lookahead assertion to skip internal from this rewrite.
Also it appears you're using a relative URL for css/js/images e.g. src="abc.png" and your current URL is: /abc/xyz then browser resolves this relative URL to http://example.com/abc/xyz/abc.png which obviously will cause a 404 since your static files are residing in /abc/app/ sub-directory.
You can add this just below <head> section of your page's HTML:
<base href="/abc/app/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.
Also as a practice to use absolute links instead of relative ones change relative link XYZ, to this one XYZ

error 404 defining URL in mod rewrite

I'm trying to rewrite my URL's to be more clean and user friendly and also better for SEO, so whenever the user clicks each country link to see the list of train journeys for each country, i.e: Italy, it should call the page country.php?country=italy , but the URL should be rewritten to great_train_journeys/country/italy.
I've tried to set rewrite rules on a .htaccess file but i'm getting the 404 error.
Here is my code for the .htaccess file:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^country=(.*)$
RewriteRule ^country/([A-Za-z0-9-]+)/?$ country.php?country=$1 [NC,R]
I'm using XAMPP to work on my local server, so my project folder is inside the HTDOCS folder, which is the root of my server:
Here is my project structure:
I've checked if mod_rewrite is enabled in the http.config file and also changed the AllowOverride to all like it is below:
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
Thanks for your help
I'm trying to rewrite my URL's from this, country.php?country=Italy to this country/Italy
I believe you meant it the other way around, at least this is what your Code tells me. So if someone enter example.com/country/italy you want that the internally the this /country.php?country=italy is called but the user should not see it.
So in this case you need:
RewriteEngine on
RewriteRule ^/?country/([^/]+)/?$ /country.php?country=$1 [NC,L]
But if you mean it the other way around so that the URL in the browser is example.com/country.php?country=Italy but this should internally go to example.com/country/Italy than you need the following:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?country=([^&]*)(&.*)?$
RewriteRule ^/country\.php$ /country/%2 [NC,L]
With this we have some small problems because if example.com/country/Italy is the real folder and someone enter example.com/country.php?country=italy that we will not find the folder with the name italy and we get a 404 error.
In your original Code you also used the [R] Flag (that means redirect), for exapmle if a user enters example.com/country.php?country=italy that the URL in the browser will change to example.com/country/italy
than you should do this:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*&)?country=([^&]*)(&.*)?$
RewriteRule ^/country\.php?$ /country/%2 [NC,R=301]
Now we do a 301 redirect.

Problems redirecting from a 403 using .htaccess (Yourls)

I'm using a php app called Yourls. It's a self-hosted url shortener and it's pretty great, I'm happy with its overall functionality. Due to the nature of its development however there isn't much in the way of support. Let's pretend the base url is af.to, where a shortened url would be af.to/goo that might redirect to whatever url is defined by 'goo'. The problem I'm facing is that if someone goes to af.to, they end up on a 403-Forbidden. I'd rather the client is redirected to a specific url instead. I have already picked up a plugin for Yourls which redirects to a url when a shortlink is not found or mis-typed, but this does not cover the base of af.to
I attempted to put in a 403 redirect in the .htaccess, but that broke the whole yourls script resulting in a 500 server error.
Current .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /yourls-loader.php [L]
</IfModule>
# END YOURLS
Any help on what I need to do?
Thank you.
The RewriteCond blocks tell the RewriteRule to skip existing files / folders. When you go to http://af.to/, the root folder exists : no redirection. The apache server doesn't find any index.html (or index.php) file, isn't allowed to list the content of the folder, give up and returns a 403 Forbidden.
You can create the index.html file to show some content or you can add these lines to redirect to an other url :
# just after RewriteEngine On
RewriteRule ^/$ http://my-compagny.com/ [L,R=301]

Specify maintenance page in .htaccess without redirecting to error page

I have to make a maintenance page for my website with the .htaccess, I've searched on the internet and could only find snippets/scripts which redirect but I only want that it displays a message on the page itself for people, but not for a specified ip-address. So when I enable the script in the .htaccess it has to show a message on every page/ file(=css, etc.) except for my ip-address
This should work.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteCond %{REQUEST_URI} !^/construction.php
RewriteRule ^ /construction.php [L]

Redirect based on part of url

How do I do an apache redirect to a startic splash page based on a dynamic url. Part of the url is always static. For example, http://buy.domain.com/product/product1-name-here
What do I need to do if I want to redirected everything that comes in http://buy.domain.com/product/* to a certain page?
Just try this simple rule in .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
RewriteRule ^product/.*$ /yourSplashPage? [R,L,NC]
In your .htaccess file, create an Apache RewriteRule to selectively choose links based on a regular expression.
RewriteRule ^product/.*$ /certainPage.html?