Redirect based on part of url - apache

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?

Related

Hide actual URL and display alternate URL using htaccess

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

Htaccess redirect or rewrite

I'm in the process of testing a site at a preview URL until the DNS transfers over. I'm previewing the site at http://IP_ADDRESS/~name/.
All of my images that have been uploaded in the CMS are listed like <img src="/uploads/images/">.
But this doesn't work while i'm on the preview URL. So ideally I need to redirect /uploads/* to /~name/uploads/*
I was wondering if this should be a redirect or a rewrite in htaccess and what the rule might be?
Thanks.
This is the code you'll need in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^uploads/ ~name%{REQUEST_URI} [L,NC]

How can I add an additional word to a URL using mod_rewrite

I have an application with URLs such as these:
https://www.domain.com/example/public/subscription
https://www.domain.com/example/public/subscription/source
https://www.domain.com/example/public/test/subscription
I'd like to use mod_rewrite (or some other method) to create shorter "aliases" of the above URLs by removing the /public/ part so that I can provide my client with these shorter versions of the URLs:
https://www.domain.com/example/subscription
https://www.domain.com/example/subscription/source
https://www.domain.com/example/test/subscription
In other words, when browsing to https://www.domain.com/example/subscription, for example:
The server must send back the same response that one would get when opening https://www.domain.com/example/public/subscription directly
The browser must still display the shorter version of the URL (without the /public/) in the address bar
Is this even possible, and how would such a RewriteRule look like?
Thank you in advance.
put this is .htaccess file in your DocumentRoot.
Options +FollowSymLinks -Indexes -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^(example)/(.*)$ $1/public/$2 [NC,L]
You just want to "remove" the public keyword?
Try this rewrite rule:
RewriteRule ^/example/public/(.*) /example/$1 [L]

Apache redirects: do not redirect if URL contains

i have a situation where i want to prevent any redirects if the URL contains "akamai"
Basically, if URL contains akamai do not redirect.
Any ideas?
Your requirements are not very clear.
Try this code in your .htaccess file:
Options -MultiViews +FollowSymLinks
RewriteEngine On
RewriteRule ^(.+)(?<!akamai)$ /akamai-$1 [L,NC]
Negative lookbehind (?akamai.

Redirecting PDF links from another domain using htaccess

We have two domains, let's call them first.com and second.com
We have a directory in second.com called reports, where all our PDFs are located, but we would like to these same PDFs accessible from first.com as well.
Can we redirect let's say first.com/reports/84839049.pdf to second.com/reports/84839049.pdf using htaccess?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^domain\.com
RewriteRule (.*) http://domain1.com/$1 [R=301, L]
Yes.
redirect /requested/url http://second.com/result/url
http://httpd.apache.org/docs/1.3/mod/mod_alias.html#redirect
You may want to consider using mod_rewrite though, unless you asked for an .htaccess configuration specifically because you have no access to the server configuration and mod_rewrite is disabled or not loaded.
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
http://webdesign.about.com/od/mod_rewrite/qt/site_redirects.htm
You'll need some grasp of regex for mod_rewrite, but it can make configuration of the redirects a lot faster than having to add a redirect for every file on your site(s).