I need to add Rewrite/Redirect statements in my .htaccess to redirect a specific url to a different .php page with masking. The goal is when someone hits:
www.mydomain.com/sandbox
i need it to go to:
server.mydomain.com/directoryname/phpprogram.php. But I need it to mask so the address bar will still show www.mydomain.com/sandbox
Appreciate any help with this. Thanks.
You can use mod-rewrite to maks your URLs.
The following rule in an htaccess file should do the trick :
RewriteEngine On
RewriteRule ^sandbox/?$ /directoryname/phpprogram.php [L]
This will internally map a request for example.com/sandbox to example.com/directoryname/phpprogram.php .
The /? in the regular expression pattern above matches the traling slash as an optional character so the rule will also match example.com/sandbox/ .
Related
I moved an old website to a new cms and some content have links that cannot be found on the new system.
For instance in the old system there was a link called https://example.com/newsletter/1234/123
in the new one i do not require those ids at the end. Therefore I would like to redirect the user to
https://example.com/newsletter/ directly.
I wrote the following in my htaccess file:
RewriteRule ^(.*newsletter)(.*) $1 [L,R=301]
This gives me unfortnately a "too man redirects" error.
Can anyone point out the error that I made?
I planned to use the regex to capture all links that contain the name "newsletter" and remove the rest of the url to redirect the user. Any help is appreciated thanks.
Using (.*newsletter)(.*) causes the rewrite to match /newsletter alone because .* matches zero or more characters. This sends mod_rewrite into a loop.
Instead, you can use .+ which matches one or more characters, and to that I would prepend /? to optionally match a trailing slash. There is no need to capture it in () because you do not reuse it as $2.
RewriteRule ^(.*newsletter)/?.+ $1 [L,R=301]
Your example began with .*. If you actually need that, leave it in. But if you are really just trying to match /newsletter/123/1234 and not /someother/newsletter/123/1234, simplify it with:
RewriteRule ^newsletter/?.+ /newsletter [L,R=301]
When you test this, use a private/incognito browsing window or a fresh browser. Browsers aggressively cache 301 redirects and it can make it very hard to debug rewrite rules if you are fighting against the cache.
My website is not stripping the text in * condition.
Website URL is like http://www. domain.com/search/adasd
I want to redirect it to https://www. domain.com/noindex-page
Using below redirect rule
RewriteRule ^search/\*$ https://www. domain.com/noindex-page? [L,R=301]
but it is redirecting to https://www. domain.com/noindex-pageadasd and giving 404 not found.
Please suggest how to strip adasd from this.
Using * is necessary as there are number of URLs with this prefix.
Based on your shown samples could you please try following. I believe problem in your regex \* is you have escaped * which means it is NO longer matching everything after search keyword and is treated as a literal as * hence it may not be working. Since you are looking for anything after search keyword then we could simply check if uri starts from search here.
RewriteEngine ON
RewriteRule ^search https://www.yourdomain.com/noindex-page? [L,R=301,NC]
please help me with redirect in .htaccess
Iam have simple link
http://domain.com/test123/image-123.gif
Iam need to redirect from this image to script.
http://domain.com/test.php?company=test123?image=image-123.gif
My example doesnt work =(
RewriteEngine On
RewriteRule ^(.+)/(.+).\gif$ tracking.php?company=$1&email=$2 [L,QSA]
Assuming this is literally what you have in a .htaccess file, the problem is that you have .\g rather than .g -- that is, you've escaped the 'g' character, rather than the . character.
That is, what you want is:
RewriteRule ^(.+)/(.+)\.gif$ tracking.php?company=$1&email=$2 [PT,L,QSA]
Also, note that this does not redirect, as you say in your question, but it does a transparent rewrite. If you want it to redirect, replace the PT with a R in the flag.
I am hoping someone can help with an unusual situation.
I have one main rewrite rule in place in my httpd.conf file which handles all of our dynamic content. The rule looks like this and works fine:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
The problem I have is that when I try to use .htaccess to create a simple 301 redirect, the query parameters are automatically appended to the end of the URL's so the final result looks like this:
http://www.example.com/category/page.html?&a=/category/subcategory/something/d/page/
Notice that the query string is appended to the URL when using .htaccess to create a 301 redirect.
I have solution for this on a case-by-case basis, but it's not practical to create a new rule each time I want to do a simple 301 redirect.
So, I am wondering if I can edit my "main rule" in any way so that when .htaccess is used to create redirects, the query parameters are not appended to the target URL.
Thanks in advance for any help you can provide.
If you have multiple simple redirects for which you want to suppress query string values you could put all the redirects in a RewriteMap (since you already have access to httpd.conf), and have one .htaccess rule that suppresses the query strings as below
place in htaccess
#if there is a match in the map
RewriteCond ${redirect_map:$1} !=""
RewriteRule ^(.*)$ ${redirect_map:$1}? [R,L]
place in httpd.conf
RewriteEngine On
RewriteMap redirect_map txt:/usr/local/apache/conf/redirect.map
contents of /usr/local/apache/conf/redirect.map
key followed by a space followed by target
directory/subdirectory1/subdirectory2/ example/category7/subdirectory/file.html
directory4/subdirectory2/subdirectory9/ example/category5/subdirectory4/file332.html
That's what your rule has defined it to do:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?\&a\=$2
It says to create a URL that will look like:
category/refine.cgi?&a=/foo/bar/
If you don't want that to happen, change your rule to be:
RewriteRule ^(.)(/./d/[^.]*)$ /category/refine.cgi\?
I'm need to redirect a a bunch of URL's through mod_rewrite. The URL structure is as follows:
www.mysite.com/somescript.php?&lang=asp&variable1&variable2
Needs to redirect to
www.mysite.com/somescript.php?&lang=php&variable1&variable2
So, basically, any URL with &lang=asp in it needs to be redirected to exactly the same URL but with &lang=php replacing &lang=asp.
Is there a way I can do this through .htaccess, perhaps with some sort of wildcard?
Thanks alot, I would appreciate your help.
Cheers,
Matt
Modifying the Query String
Change any single instance of val in the query string to other_val when accessing /path. Note that %1 and %2 are back-references to the matched part of the regular expression in the previous RewriteCond.
RewriteCond %{QUERY_STRING} ^(.*)lang=asp(.*)$
RewriteRule /path /path?%lang=php%2
Read this page for more info http://wiki.apache.org/httpd/RewriteQueryString