Redirecting URL with (Brackets) - apache

does anyone know how I can redirect a URL with brackets? I made a mistake a while back and we have since changed to a proper url format but the link with URL remains indexed in Google.
I've tried the following but none seems to be working
RewriteRule ^url-with-(brackets)(.*)$ http://url.com/url-with-no-brackets$1 [R=301]
RewriteRule ^url-with- \brackets\ (.*)$ http://url.com/url-with-no-brackets$1 [R=301]
Thanks in advance.

Give this a try:
RewriteRule ^(.+)\((.+)\)(.*)$ /$1$2$3 [R=301,L]
This will extract the entire text without the parenthesis.
Keep in mind that you may have been cached from previous attempts since you are using 301 so I recommend you that you test it using a different browser just to make sure its working.

Related

htaccess redirect to a new page but keeping "get" parameter

I need to do a simple htaccess redirect to a new page and at the same time rename the "get" parameter. The original url is: www.example.com/display_item.php?x=2735 and I want a redirect to:
www.example.com/product.php?productcode=2735.
Here is my latest attempt (of many) but this does not work and I get a 404 back:
RedirectMatch 301 /display_item.php?x=(.*)$ /product.php?productcode=$1
I think I am very close to solving this but I am pulling out my hair at the moment! I have looked on Stack Overflow for a solution, but nothing I have found works.
As mentioned in the comment, the query string is not part of the URI. A such, you will need to use mod_rewrite to check the query string separately (using %{QUERY_STRING} in a condition).
RewriteEngine On
RewriteCond %{QUERY_STRING} ^x=(\d+)$ [NC]
RewriteRule ^display_item.php$ /product.php?id=%1 [L,R=302]
Change 302 to 301 to make the redirect permanent.
Note: In future, please review the relevant documentation (in order to get a better understanding of how everything works) before asking here on SO.

Apache .htaccess redirect URL directories containing parameters

Sorry to be the asker of yet another tedious mod_rewrite question but after having made no progress in the last few hours, I thought it was time to ask ;)
I am trying to redirect URLs like these:
/some/thing?a=1 --> http://something-else.com/blah
/some/thing?a=1&b=whatever --> http://something-else.com/blah2
No need to keep the param values - the new URL will be hard-coded for each one I have to be redirected.
Have tried a few different things from other posts but with no joy so I am back to square one so any suggestions would be most welcome.
Thanks! :)
You can use the following rule-set:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^a=1$
RewriteRule /some/thing http://something-else.com/blah [L]
This is indeed quite a common question, and people tend to overlook the QUERY_STRING variable. Have you tried it before?
This is what I used in the end:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(a=1&b=whatever)$
RewriteRule /some/thing http://something-else.com/blah2 [L,R=301]
I was adding the rules at the bottom rather than directly below the "RewriteEngine On" which was preventing it from working.
This solution does still append the params to http://something-else.com/blah2 which isn't exactly what I wanted but it will do.

.htaccess mod_rewrite - Non-matching patterns

I'm having trouble getting non-matching (negation) mod_rewrite patterns to work.
In the example below I want all requests redirected to "/promo-page", however, don't allow a redirect loop (hence the RewriteCond).
### Request URL: http://example.com/promo-page ###
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !/promo-page
RewriteRule .* /promo-page [R=302,L]
When a request for "/promo-page" is made, the RewriteCond rule is ignored and a redirect loop occurs.
Any ideas why the Exclamation Mark (!) isn't working? Is the syntax incorrect?
Thanks in advance.
UPDATE
I've also tried these rules in an online mod_rewrite tester and they also don't work (if you use "home" as the URL and "/home" as the REQUEST_URI).
http://martinmelin.se/rewrite-rule-tester/
Remove RewriteCond %{REQUEST_URI} !^/$ - it will always be true, as Jon says.
Thanks all for your answers and thoughts. I finally tracked down the issue, so here's the details in the hope it may help someone else down the track.
What was confusing the situation was more rules further down in the .htaccess file. These weren't posted in my original question as I didn't believe they were relevant and would even affect the outcome.
These additional rules re-write the Clean URL's into a GET request. I was under the (false) impression that %{REQUEST_URI} also includes the GET variables tacked onto the end of URL's. It doesn't. You must use %{QUERY_STRING} to separately check the content of the GET query string.
Mod-Rewrite Logging was very useful in helping me debug this issue. I wasn't able to enable it on our live/production cPanel server, so I installed XAMPP on my PC, enabled logging and played with the rewrite rules locally until I got it working.

Rewrite rule without changing URL in browser

I want a rewrite rule to work in such a way that it loads the second URL but in browser address field it should show first URL.
For example domain.com/folder1/folder2 should load domain.com/folder1 but not show domain.com/folder/folder2 in browser.
I tried this but it basically changes the URL in browser.
RewriteRule ^/folder1/folder2(.*)$ /folder1/$1 [L]
Tried googling but didn't get any help. Appreciate your help!
Remove the beginning /s from the rule:
RewriteRule ^folder1/folder2(.*)$ folder1/$1 [L]

The correct way to make a rewrite rule?

I have a .htaccess in my site www folder that has this rewrite rule:
RewriteRule ^(\w+)/?$ /$1.php
It works, if you type in
http://sampardee.com/urltest -
It finds urltest.php and brings it up.
However, if you type in
http://sampardee.com/urltest/
it still brings urltest.php up but the CSS stops working. I have the CSS file specified in a link tag. The same results appear also when
http://sampardee.com/urltest.php/
is accessed.
Is there any way I can fix this so that someone could type in
http://sampardee.com/urltest/
and have urltest.php come up, but yet still display the linked CSS file?
Please help :)
-Sam
The problem isn't with mod_rewrite, but with the css link (the browser tries to fetch http://[...]/urltest/css/default.css instead of /css/default.css).
Try adding a beginning slash, and changing the to:
/css/default.css
A better idea would probably be to redirect http://sampardee.com/urltest/ to http://sampardee.com/urltest.
RewriteRule ^(\w+)/$ /$1 [R]
RewriteRule ^(\w+)$ /$1.php