Wildcard redirect with .htaccess - apache

I would like to redirect this pattern with .htaccess:
https://www.example.com/abcdef1/products
https://www.example.com/abcdef2/products
https://www.example.com/abcdef3/products
to
https://www.example.com/abcdef1/
https://www.example.com/abcdef2/
https://www.example.com/abcdef3/
How can I achieve that?

Could you please try following, written as per shown samples. After placing these Rules into your .htaccess file, make sure you clear cache of your browser before testing it.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/abcdef[0-9]+/?$ [NC]
RewriteRule ^(.*)$ $1/products [L]

Related

Redirecting index.php?p= to product.php?id= using htaccess

I know it could be easily solved i just looked everywhere and tried everything without sucess so i had to ask here ;
how do i 301 redirect index.php?p=(number) or /?p= to product.php?id=(number) with htaccess.
I thought this might work but it didnt
RewriteEngine on
RewriteBase /
RewriteRule ^(index.php|?)p\=([0-9]+)$ product.php?id=$2 [R=301]
With your shown samples/attempts, please try following htaccess rules. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{THE_REQUEST} \s/index\.php\?p=(\d+)\s [NC]
RewriteRule ^ product.php?id=%1 [R=301,L]

.htaccess redirect only if GET parameter exist

I have the following line in my .htaccess file:
Redirect 301 /folder1 https://www.example.com/folder2/file.php
This will redirect everything from /folder1 to https://www.example.com/folder2/file.php.
I need a condition to only allow this redirection if the URL contains a mykey= GET parameter, else ignore this redirection command.
How can I do that?
You cannot do this using Redirect directive that does basic URI matching.
You will need to use mod_rewrite based rules for this like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)mykey= [NC]
RewriteRule ^folder1(/|$) /folder2/file.php [R=301,L,NC]
Make sure to clear your cache before testing.
References:
Apache mod_rewrite Introduction
I finally found the a working solution:
RewriteCond %{REQUEST_URI} ^/folder1/
RewriteCond %{QUERY_STRING} mykey=
RewriteRule ^folder1\/$ /folder2/file.php$1 [R=301,L]

How to implement ssl on everything except a specific and/or a few specific files/folders using .htaccess

I am implementing SSL on everything but would like to exclude a specific page or specific set of pages from using SSL. How can I do this. At the moment I am using the simple syntax:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://mydomain/myfolder/$1 [R,L]
If you could could you explain the steps you are taking in order to do this please so that I can actually understand why and what you have done and I will learn something from this?
Thanks for the help,
John
You can use it like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/(excluded1|excluded2)/ [NC]
RewriteRule ^(.*)$ https://mydomain/myfolder/$1 [R,L]
RewriteCond %{REQUEST_URI} ! is for excluding listed paths in https redirection.

How to get a clean htaccess redirection erasing the url parameters

I am struggling with my .htaccess to have some clean output.
I have the link:
http://googleads.g.doubleclick.net/aclk?sa=L&ai=C6L7ifLUkUqDLHMLk8AOStYCIA_Te4_sDAAAQASAAUN-djNP4_____wFg-5n5gogKggEXY2EtcHViLTg5Mzg3NzAyNjUxMTc1MTXIAQLgAgCoAwGqBIIBT9D5uNN_cW90jRk-X4AGFEvVUc3uXa1gEO0EVdqEJdFyscP9bI0bJppGDdfQaHLHr7d8O0OCuV7YjpyvQksPhaVEtZ5itfPvt-VGEgDXYEYXKaUOI7F5keBCdOwMtKYRQ_idwvU18o5TiLhZlIn3YEPi84R2VHemIxwO1Nrffrubl-AEAaAGFA&num=0&sig=AOD64_0k1kyhYI-Pe4qbk_16igqgBYXrqA&client=ca-pub-8938770265117515&adurl=http://THAT_ADVERTISMENT
When I click on it I would like to be redirected to:
http://stackoverflow.com/
My .htaccess is:
#Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^googleads.g.doubleclick.net$ [OR]
RewriteCond %{HTTP_HOST} ^adclick.g.doubleclick.net$
RewriteRule ^(.*)$ http://stackoverflow.com/ [R=301,L]
The issue here is the redirected url is like:
http://stackoverflow.com/?sa=L&ai=C6L7ifLUkUqDLHMLk8AOStYCIA_Te4_.....
I would like to have a clean output without the parameters after the first slash. I read some stuff and I test my htaccess with an online tester.
Please excuse me if my question is too candid, maybe I have read too fast my documentation. Anyway, thank you in advance for your help/solution/link :).
RewriteRule ^(.*)$ http://www.stackoverflow.com/? [R=301,L]
This will redirect to this 3rd party

.htaccess in subdomain

I'm trying to set up a subdomain as follows:
http://subdomain.domain.com should load what is really http://subdomain.domain.com/index.php/contest/
http://subdomain.domain.com/stepone should load what is really http://subdomain.domain.com/index.php/contest/stepone
http://subdomain.domain.com/images/example.jpg should load http://subdomain.domain.com/images/example.jpg as normal.
I've had this all set up and working in a subdirectory on the main domain before but now that I'm trying to do it on a subdomain it's stopped working.
Here is my .htaccess:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|stylesheets|javascript|email|robots\.txt|test\.php|terms\.html)
RewriteRule ^(.*)$ /index\.php/contest/$1 [L]
Please help me!
Are rewrite rules working at all? I.e. do you have AllowOverride All or something for virtual hosts, as per:
http://www.webhostingtalk.com/showthread.php?t=481815
Also, take a look this comment by Gumbo on this SO question:
Htaccess help, $1 being used before its set??? what?
The Drupal .htaccess file is a good example of mapping /?q=query to /query, but not redirecting things which provide an explicit file/directory match. Here's the relevant snippet from Drupal's .htaccess with ?q= changed to index.php/contest/.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/contest/$1 [L,QSA]
If you need to restrict the domain so that it doesn't redirect normally but just from the one domain, insert before the RewriteRule this:
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.com$ [NC]
I think I have found the answer using:
RewriteRule ^(.*)$ subdomain.domain.com/index\.php/contest/$1 [L, P]
Is there any reason I shouldn't use the force proxy like this? (Does it slow things down?)
Thanks for your answers guys.