replace all instances of string in htaccess - .htaccess

Simple question here.
I need to replace all instances of a string in the URL with another string.
IE. I need to replace every instance of "test" in the URL, with .htaccess code.
For example "www.example.com/test/A/test/B/test" will become "www.example.com/REP/A/REP/B/REP".
I can produce a script that replaces "test" with "REP", when "test" is not at the end or the beginning of the script. But I cannot come up with a final script to replace "test" everywhere.
My code:
RewriteRule ^test\/(.*) REP/$1 [L,R=301]
Thanks!

I found an answer, that is all knowing and beatiful.
RewriteRule ^(.*)test(.*)$ $1FIN$2 [R=301,L]

Related

mod_rewrite pass variable and

What I would need to do is to pass 1 variable as variable, and the rest as a rest of URL intact, so I can get them by $_GET in php later. The below does not work:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html?(.*)/?$ store.php?var1=$1&$2 [L]
Possible Links could be:
store/products.html
store/products.html?sort=asc&price=down
store/products.html?price=down&here_we_can_have_a_lot_of_different_params_in_whatever_order
Basically, just take this $var1 and the rest forward to URL? How can I do that?
P.S. I think I found a solution:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html?(.*)/?$ store.php?var1=$1&%{QUERY_STRING} [L]
The problem is your premise as to how a RewriteRule works. A RewriteRule only matches against the URI path (in apache terms the Request URI) and not against the query string. This means the (.*)/?$ at the end of your regex only works because it can reduce to just '$' (or the end of the string) and the ? before it just means the regexp will match .htm as well as .html
The simpler version of your rule is as follows:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html store.php?var1=$1 [L,QSA]
The QSA stands for Query String Append, which simply adds back any existing query string to the rewritten URL.

mod_rewrite .htaccess with %20 translate to -

I have been reading about .htaccess files for a couple of hours now and I think I'm starting to get the idea but I still need some help. I found various answers around SO but still unsure how to do this.
As far as I understand you write a rule for each page extension you want to 'prettify', so if you have something.php , anotherpage.php, thispage.php etc and they are expecting(will receive??) arguments, each needs its own rule. Is this correct?
The site I want to change has urls like this,
maindomain.com/sue.php?r=word1%20word2
and at least one page with two arguments
maindomain.com/kevin.php?r=place%20name&c=person%20name
So what I would like to make is
maindomain.com/sue/word1-word2/
maindomain.com/kevin/place-name/person-name/
Keeping this .php page and making it look like the directory. Most of the tutorials I have read deal with how to remove the .php page to which the argument is passed. But I want to keep it.
the problem I am forseeing is that all of the .php?r=parts of the url are the same ie sue.php?r=, kevin.php?r= and the .htaccess decides which URL to change based on the filename and then omits it. If I want to keep the file name will I have to change the ?r=
so that it is individual? I hope this make sense. So far I have this, but I'm sure it won't work.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1
RewriteRule ^([a-zA-Z0-9]+)/$1.php?r=$1&c=$1
And I think I have to add ([^-]*) this in some part or some way so that it detects the %20 part of the URL, but then how do I convert it to -. Also, how are my $_GET functions going to work??
I hope my question makes sense
You're missing a space somewhere in those rules, but I think you've got the right idea in making 2 separate rules. The harder problem is converting all the - to spaces. Let's start with the conversion to GET variables:
# check that the "sue.php" actually exists:
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/?$ /$1.php?r=$2 [L,QSA]
RewriteCond %{REQUEST_URI} ^/([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^([a-zA-Z0-9]+)/([^/]+)/([^/]+)/?$ /$1.php?r=$2&c=$3 [L,QSA]
Those will take a URI that looks like /sue/blah/ and:
Extract the sue part
Check that /document_root/sue.php actually exists
rewrite /sue/blah/ to /sue.php?r=blah
Same thing applies to 2 word URI's
Something like /kevin/foo/bar/:
Extract the kevin part
Check that /document_root/kevin.php actually exists
3 rewrite /kevin/foo/bar/ to /kevin.php?r=foo&c=bar
Now, to get rid of the "-" and change them to spaces:
RewriteCond %{QUERY_STRING} ^(.*)(c|r)=([^&]+)-(.*)$
RewriteRule ^(.*)$ /$1?%1%2=%3\ %4 [L]
This looks a little messy but the condition matches the query string, looks for a c= or r= in the query string, matches against a - in the value of a c= or r=, then rewrites the query string to replace the - with a (note that the space gets encoded as a %20). This will remove all the - instances in the values of the GET parameters c and r and replace them with a space.

Can I use htaccess to Remove a phantom ? from the URL

Google is somehow indexing some phantom URL's for my website. I'm still trying to find the problem.
For example, everything in bold below is NOT supposed to be there.
http://www.mydomain.com/directory/sights?/kauai/sights/sights/kalalau_lookout
It should look like this:
http://www.mydomain.com/directory/sights
How can I strip out the ? mark in addition to everything after it and make it 301 redirect to the proper page?
Thanks,
John
From mod_rewrite
Modifying the Query String
By default, the query string is passed through unchanged.
...
When you want to erase an existing query string, end the substitution string with just a question mark.
...
You can remove all query strings with
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule .* http://www.mydomain.com$0?

How to write this .htaccess rewrite rule

I am setting up a MVC style routing system using mod rewrite within an .htaccess file (and some php parsing too.)
I need to be able to direct different URLs to different php files that will be used as controllers. (index.php, admin.php, etc...)
I have found and edited a rewrite rule that does this well by looking at the first word after the first slash:
RewriteCond %{REQUEST_URI} ^/stats(.*)
RewriteRule ^(.*)$ /hello.php/$1 [L]
However, my problem is I want it to rewrite based on the 2nd word, not the first. I want the first word to be a username. So I want this:
http://www.samplesite.com/username/admin to redirect to admin.php
instead of:
http://www.samplesite.com/admin
I think I just need to edit the rewrite rule slightly with a 'anything can be here' type variable, but I'm unsure how to do that.
I guess you can prefix [^/]+/ to match and ignore that username/
RewriteCond %{REQUEST_URI} ^/[^/]+/stats(.*)
RewriteRule ^[^/]+/(.*)$ /hello.php/$1 [L]
then http://www.samplesite.com/username/statsadmin will be redirecte to http://www.samplesite.com/hello.php/statsadmin (or so, I do not know the .htaccess file)
To answer your question, "an anything can be here type variable" would be something like a full-stop . - it means "any character". Also the asterisk * means "zero or more of the preceding character or parenthesized grouped characters".
But I don't think you need that...If your matching url will always end in "admin" then you can use the dollar sign $ to match the end of the string.
Rewrit­eRule admin$ admin.php [R,NC,L]
Rewrites www.anything.at/all/that/ends/in/admin to www.anything.at/admin.php

Create a rewrite rule based on querystring

I have many redirects in the .htaccess file. Now, I need to create a rewrite rule for a URL based on it's querystring value..
http://www.mydomain.com/subdir/RentalDetails.aspx?RentalId=1072
any traffic to this url should go to...
http://www.mydomain.com/subdir/RentalDirectory.aspx
I have researched and found some good posts on the subject and came up with this..
#discontinued properties
RewriteCond %{REQUEST_URI} ^/RentalDetails\.aspx$
RewriteCond %{QUERY_STRING} ^RentalId=1072$
RewriteRule ^(.*)$ /subdir/RentalDirectory.aspx? [R=301,L]
Unfortunately, it is not working. Could someone please help me to figure out where I have gone wrong? Many thanks in advance!
I'd guess that the RewriteCond is wrong: the uri shouldn't look like
^/RentalDetails\.aspx$
[Edited -- I finally managed to get to the actual docs, and found the query string was a red herring. However, I missed the '^' at the start of the string as well, which may be causing this]
Since the ^ at the start of the expression means "the string must start here", and the $at the end means "the string ends here", your regex is too restrictive, and won't match what you need it to.
From the apache docs, the REQUEST_URI variable excludes the query string, so that's not the issue. However, you are trying to match a url of /subdir/RentalDetails.aspx with a regex that has to start with the word /RentalDetails
Try something like
# No ^ at the start, this will match RentalDetails.aspx in any location
/RentalDetails\.aspx$
Or
# This will only match in the /subdir/ directory.
^/subdir/RentalDetails\.aspx$

Resources