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

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?

Related

Htaccess redirect long urls

I've got problem with redirecting old google maps urls to new one. I got new links (after component update), but i want make old ones work.
this is old one
domain.com/index.php?option=com_xmap&sitemap=1&view=xml
this is new one
domain.com/index.php?option=com_xmap&view=xml&tmpl=component&id=1
Please, help me redirect this :)
I think the following redirect would work. It redirects when there is no id= in the query string, and appends id=somenumber to the query string where somenumber is the number behind sitemap.
RewriteCond %{QUERY_STRING} !^(.*&|)id=
RewriteCond %{QUERY_STRING} ^(.*&|)sitemap=([0-9]+)(&.*|)$
RewriteRule ^ %{REQUEST_URI}?id=%2 [QSA,R,L]
A quick rundown of what I used here. %{QUERY_STRING} is a variable, just as %{REQUEST_URI}. A ! before a regex will negate that regex (ala 'doesn't contain'). %2 is replaced with the second capturing group of the last RewriteCond. In this case it will get replaced by whatever is captured by ([0-9]+). The flags are QSA (query string append), R (redirect, default is temporary redirect) and L (if this rule matches, stop rewriting; this doesn't affect the new request that is made). The documentation can be found here.

.htaccess not working as expected in sub-folder

I have this rewrite rule placed in /dashboard/.htaccess [dashboard is actually a folder]:
RewriteRule ^([^/]*)$ index.php?mode=$1 [L]
My structure is index.php?mode=support, even though, $_SERVER['QUERY_STRING'] outputs this:
mode=index.php
Example: site.com/dashboard/index.php?mode=support should be site.com/dashboard/support
So , how can I make it parse the param value, and not the file itself.
Managed to solve it while doing more research on regular expressions.
RewriteRule ^([a-z]+)$ index.php?mode=$1 [L,QSA]
Thi solved my problem, preferred plus instead asterisk because it tells the engine to repeat it zero or more times. (when i'm on index.php , query string is empty as needed)
Your rule is matching anything that starts with not a slash and doesnt contain a slash anywhere when your actual path is
/dashboard/support
to get the folder you actually want you need a base on there like this
RewriteBase /dashboard/
If that is placed above the rule, Then your redirect should be ok

Why does my RewriteRule not work when there is a `?` in the URL

I am learning how to write regular expressions for .htaccess redirects.
So far I've managed to figure out everything I needed, except for a couple of regular expressions which don't behave as I expected. I am testing my regular expressions using a desktop application, and they work fine there, but not in the .htaccess file.
FYI: The RewriteBase is set to /site/
This is the incoming URL:
/site/view-by-tag/politics/?el_mcal_month=3&el_mcal_year=2009
I want to grab "politics" and redirect to /site/tags/politics/
Here is what I used:
RewriteRule ^view-by-tag/([a-zA-Z\-]+)/([a-zA-Z0-9\-\/\.\_\=\?\&]+) /tags/$1/ [R=301,L]
I added the capture of all the characters after politics because I am having the issue that when there is a ? in the URL the redirect does not work, and I can't figure out why. In the URL given above, if I remove the ? it works fine, but if the ? is in there, nothing happens. Is there a reason for this?
The same thing happens when I try to capture 307 from /site/?option=com_content&view=article&id=307&catid=89&Itemid=55
I used this regular expression, article&id=([0-9]+) /?p=$1 [R=301,L] but again, when there is a ? in the URL it stops the redirect for doing anything.
What is the reason for that?
The .htaccess file in question is on a Wordpress blog (3.4.1)
The point that you've missed is that the rewrite engine splits the URI into two parts: the REQUEST_URI and the QUERY_STRING. The query string part isn't used in the rule match string so there is no point in constructing rule regexp patterns to look for it.
You can probe and pick out parameters from the query string by using rewrite conditions and condition regexps to set %N variables.
By default the query string is appended to the output substitution string unless you have a ?someparam in it -- in which case it is ignored unless you used the [QSA] (query string append) parameter.
The way that you'd pick up the id in /site/?option=com_content&view=article&id=307&catid=89&Itemid=55 is to use something like:
RewriteCond %{QUERY_STRING} \bid=(\d+)
Before the rule and this would set %1 to 307. Read the rewrite documentation for more general discussion of how to do this.
The query string is must be processed separately in a RewriteCond if you need to manipulate it, and should not be matched inside the RewriteRule Instead, just match the request not including the query string, and use QSA to append the query string onto the redirect:
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/ [R=301,L,QSA]
# OR, if you don't want the rest of the query string appended, put a `?` onto
# the redirect to replace it with nothing
RewriteRule ^view-by-tag/([A-Za-z-]+)/?$ /tags/$1/? [R=301,L]
Actually, the QSA may not be needed in a R redirect - I think that the default behavior is to pass the query string with the redirect.
If you need to capture 307 from the query string, do it in a RewriteCond and capture in %1:
# Capture the id in %1
RewriteCond %{QUERY_STRING} id=([\d]+)
# Redirect everything to /, pass %1 into p
RewriteRule . /?p=%1 [LR=301,L]

Partial URL redirect with .htaccess

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

301 Htaccess RewriteRule Query_String

Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they need to be forwarded to website.com/#Videos:133r534|213213|12312312
What I've tried: During the last hours I tried many mod_rewrite (.htaccess) rules with using Query_String, all failed. The last message in this topic shows a solution for this problem, but what would be the rule in my situation.
I'm very curious how you would solve this problem :)!
The following will handle the simple case you show. You'll need to add additional logic if you need to allow for other parameters in the query string or file names before the ?.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^i=(.*)
RewriteRule ^.* /#Video:%1? [NE,R=permanent]
Why is this tricky?
RewriteRule doesn't look at the query string, so you have to use RewriteCond to evaluate the QUERY_STRING variable and capture the part you'll need later (referenced via %1)
the hash character (#) is normally escaped, you must specify the [NE] flag
The trailing ? on the substitution string is required to suppress the original query string
I tested this on Apache 2.2.

Resources