Removing query strings in htaccess with conditions - regex

I'm about ready to pull my hair out on this. Basically, I've changed an old .asp site (that used queries for products) to a .php site (that also uses queries, but for different parameters). Trying to get my htaccess redirects to work. The bottom line is that I need to pull the query strings out at certain times, but not others.
Here is the form of the old URL for a product, product ID in this case being "101":
http://www.example.com/shopping/shopexd.asp?id=101
There were a lot of products, and as my new site does not use queries for products, so I just want to redirect them all to my new home page.
If I use something like this:
RedirectMatch 301 ^/shopping/(.*) http://www.example.com/
Then it leaves the ?=101 at the end, giving me:
http://www.example.com/?=101
I can't use a blanket "get rid of all queries" type of approach because the new site does use them for other things, like order ids or category ids.

You need to use mod_rewrite rule to strip existing query string.
put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteRule ^shopping/ http://www.mysite.com/? [L,NC,R=301]
Here ? in the end is used to strip off any existing query string.

Related

Use RegEx to redirect using data from files

Recently, we restructured a large site of one of our customers. This caused all the news-articels on that site to be on a different place. Problem is that the google cache is still showing them on the old location, leading to A LOT of 404 not founds ( its about 1400 news entries ).
Normally, a redirect using somewhat simple regex would be fine, but not only the path to the news did change, but also some parameters. Example:
Old Url:
http://www.customers-url.com/old/path/to/the/news/details/?tx_ttnews%5Btt_news%5D=67&cHash=a782f3027c4d00face6df122a76c38ed
How the new url should look like:
http://www.customers-url.com/new/path/to/news/?tx_news_pi1%5Bnews%5D=65
As you can see, the parameter D did change from 67 to 65 and the part of the URL before the ? did also change. Also, tx_ttnews has changed to tx_news and tt_news changed to news and the &cHash part did fall away completely.
I have the changed ids in a csv in the following format:
old_id, new_id
1,2
2,3
...etc...
Same goes the the changed url part before the ?. Since im not exactly an expert in using regex my question is:
Can this be done inside the .htaccess using RegEx ( not sure if it can even use a file as input)? How complicated is that? And how would such a regular expression look like?
Rather than trying to use .htaccess, it would be easier to manage and easier to code if you simply make a new page that responds on the old url (/old/path/to/the/news/details), then make that page build the new url and return a 301 to the browser with that new url.

301 redirect to correct url

I have a lot of incorrect bad links and want to 301 redirect them to the correct one, the correct url are as follows:
Blockquote http://www.domain.com/string-video_string.html
the back links are pointing to:
Blockquote http://www.domain.com/string_string.html
any possible way to 301 redirect the wrong back links to the correct links?
Thank you in adance
You can use this rule in your site root .htaccess:
RedirectMatch 301 ^/([^_-]+)_(.+)$ /$1-video_$2
Depending on how you want to redirect (in which method; PHP, htaccess, etc.) you have some options.
I assume you're seeing 404 errors when users are trying to get to the links from an external source, like a search engine.
If that's the case, you can easily generate the code you need for which ever method you choose using this website:
http://www.rapidtables.com/web/tools/redirect-generator.htm
Make sure that you correctly format the URL's you want to redirect and it should work fine.
If you want to make sure your SEO issues get fixed, you should create a robots.txt file and place it in the root directory of your site (usually where the index file is) - and follow the instructions from this site: http://tools.seobook.com/robots-txt/ to de-index the bad links from the search engine. You may also want to create and submit (or resubmit) XML site maps to the search engines your users use most.

How do i change the base url of my application from : http://test.something.com/ to http://test.something.com/abc.cfm

The url : http://test.something.com/ is the login page of the application. By default it calls a page 'login.cfm'. But it doesnt shows in the url. May be it is defined in the server file as a default page.
What I want is, when i access this url [http://test.something.com/], the url should also show the called page i.e. the url should look like : http://test.something.com/login.cfm everytime.
I dumped the cgi variables and below is the stack for the same. I was wondering whether i have to change the cgi.http_referer or cgi.http_host.
Or do I need to change some file on the server side? I am using Apache.
EDITED:
what you are asking might be a browser dependent issue. In the Browser there is option to trim the URL from the address bar, which might be the case for you.
Please check this link and get it confirmed.
http://www.cnet.com/how-to/how-to-show-the-full-url-in-firefox/
Still if you want to change your application's URL to look something else then you can try "URL rewrite"
You can check this link for URL rewrite.
http://www.iis.net/learn/extensions/url-rewrite-module/user-friendly-url-rule-template
(You forgot to show the cgi scope dump, fyi)
Are you certain that Application.cfc/Application.cfm isn't just showinng the login page? Also look in any header files. That's the likely scenario always showing the login page.
Anyway, you can use .htaccess to do just this but it's not clear whether you want each subdirectory to its index.cfm, or if you want to redirect to the root.
If you want the redirect to the index page of the current directory (/something/ redirects to /something/index.cfm)
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^((.*)$) /$1/index.cfm [L,R=301]
If, instead, you want it to redirect to the root folder, that's a pretty simple change
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^((.*)$) /index.cfm [L,R=301]
The L in both examples stops processing of rules after that, so if you have other rules in .htaccess, that may be a concern.
The R flag (R=301) is important to cause the URL to visibly change, creating the effect you want.

.htaccess Redirect New URL with ALL Query Strings

Been reading much Q&A about this in StackOverflow, but can't seem to find the solution I need.
I have an old url that has many possible query strings / parameters appended to it (too many to do line-by-line matching). I have a new url that is going to handle these requests but need to 301 redirect to the new url with any/all parameters intact...
oldscript.php (many combinations of parameters could be appended to the inbound url)
newscript.php (append ? and any parameters found with oldscript.php)
This must be easier than I am making it, but all the examples have me pruning parameters one at a time and there are over 20 that could be included in any order or combination or none.
Looking for an apache .htaccess way to silently redirect the inbound visitor to the new script and pass any/all query parameters along to the new script.
Thanks gurus...
Note: I have done a quick and dirty PHP mod to the top of the oldscript for now but there must be a better way...
$params = explode('?',$_SERVER['REQUEST_URI']);
header( 'Location: http://www.mysite.com/newscript.php?'.$params[1]) ; die;
A simple rule like this should take care of that:
RedirectMatch 301 "^/oldscript\.php$" /newscript.php
Note that any existing query string will automatically be carried over to the new URI.

Codeigniter Routes for filename with extension

I am using codeigniter and its routes system successfully with some lovely regexp, however I have come unstuck on what should be an easy peasy thing in the system.
I want to include a bunch of search engine related files (for Google webmaster etc.) plus the robots.txt file, all in a controller.
So, I have create the controller and updated the routes file and don't seem to be able to get it working with these files.
Here's a snip from my routes file:
$route['robots\.txt|LiveSearchSiteAuth\.xml'] = 'search_controller/files';
Within the function I use the URI helper to figure out which content to show.
Now I can't get this to match, which points to my regexp being wrong. I'm sure this is a really obvious one but its late and my caffeine tank is empty :)
You should not need to escape the full stop, CodeIgniter does most of the escaping for you.
Here is a working example I use:
$route['news/rss/all.rss'] = "news/rss";
Issue was actually in .htaccess file where I had created a rewrite exception to allow the search engine files to be accessed directly rather than routing them through codeigniter.
RewriteCond $1 !^(index\.php|google421b29fc254592e0.html|LiveSearchSiteAuth.xml|content|robots\.txt|favicon.ico)
Became
RewriteCond $1 !^(index\.php|content|favicon.ico)