htaccess url redirects with parameters not working - apache

I did a search of previous questions about URL redirects with parameters, but none seem to speak to my particular problem. I'm not a programmer so I don't really know how to adapt other suggestions to my situation. Specific HTACCESS strings to try (and adapt for other URLs) would really help me.
I did an SEO restructure of my WP blog permalinks and I am finding that although the naked URLs are redirecting OK, URLs with parameters are not redirecting, they are going to a 404 error. I need URL parameters because my site is multilingual (Transposh plugin) so the "lang" parameter tells the site what language to translate the content to.
I think I may need to create a bunch of HTACCESS redirects that will redirect old URLs with a language parameter to the new permalinks for those URLs and pass the lang parameter through.
An example of this would be:
Source URL: /this-old-postname/?lang=(*)
Destination URL /blog/this-new-postname/?lang=$
There's no way to predict the URL pattern (although the parameter pattern is predictable) as each URL was tweaked for best SEO contribution.
I expect I'll need to write lots of these, each unique, so if you are able to provide an example can you please provide it for two redirects which would work for the following actual examples?
Source: http://www.travelnasia.com/thailand/bangkok/don-mueang-airport/?lang=zh
Destination: http://www.travelnasia.com/thailand/don-mueang-airport-bangkok/?lang=zh
Source: http://www.travelnasia.com/blog/map-attractions-bangkok-skytrain/?lang=zh
Destination: http://www.travelnasia.com/blog/bangkok-skytrain-bts-mrt-lines/?lang=zh
MOD_REWRITE is already enabled and standard redirects created in HTACCESS do work. I am pretty sure to achieve this I will need to use:
RewriteCond %{QUERY_STRING} ^lang=(.*)$
I understand this ensures the query string is read from the source URL.
I thought a redirect rule like this should work, but it doesn't:
RewriteRule ^test-redirect/$ http://test.travelnasia.com/destination/hanoi/hanoi-vietnam-destination-guide/$1 [L,QSA]
I've tried lots of other combinations but none of them seem to work.
Thanks in advance for any help offered.
Tony

If you don't mind including other query string parameters, [QSA] (query string append) is by far your simplest option.
Make it a 301 redirect with [R=301] if this is permanent change.
This should work for your purposes
RewriteEngine on # if not already enabled
RewriteRule ^thailand/bangkok/don-mueang-airport/?$ /thailand/don-mueang-airport-bangkok/ [QSA,R=301]
RewriteRule ^/blog/map-attractions-bangkok-skytrain/?$ /blog/bangkok-skytrain-bts-mrt-lines/ [QSA,R=301]
# ...

Related

Htaccess to rewrite url for an existing php app

I have developed a PHP application and I want to rewrite the URLs to make it cleaner,
I have multiple pages, each page can have 0 or multiple GET variables,
Example of what I want to achieve:
index.php -> /
/index.php?var1=aaa&var2=bbb&var3=ccc -> /var1/aaa/var2/bbb/var3/ccc
/product.php?var4=ddd&var5=eee -> /product/var4/ddd/var5/eee
/products-list.php -> /products-list/
etc...
Can anyone help me with a full .htaccess file to achieve this result?
And is there a way to make the PHP read the GET params or do I need to add a function to create a $_GET array from the URL on each page?
Thank you.
These simple rules will rewrite the examples you give in the question:
RewriteEngine on
RewriteEngine ^/?products-list/?$ /products-list.php [QSA,END]
RewriteRule ^/?product/([^/+])/([^/+])/([^/+])/([^/+])/?$ /product.php?$1=$2&$3=$4 [QSA,END]
RewriteRule ^/?([^/+])/([^/+])/([^/+])/([^/+])/([^/+])/([^/+])/?$ /index.php?$1=$2&$3=$4&$5=$6 [QSA,END]
You might have to add some conditions to prevent interference with other rules or rewriting loops. But that is nothing we can know without insight into your specific situation and setup.

.htaccess Redirect based on HTTP_REFERER being empty

I'm trying to set up a redirect on a WP blog installation that will detect anyone coming in from nowhere (i.e. not from another site). The idea is to trap some of the spambots that plug pre-constructed URLs into the system to create comments/posts. I figure if they don't have a referrer site, I can pop them back to the homepage (www.domain.com/index.php or just www.domain.com), which should mess with the bots but not with real people.
I understand that the referrers can be forged but hopefully it'll stop the stupids, at least.
I have very little clue about .htaccess rewrite rules (I apologise for being a noob), but I couldn't find one that did this in existing answers or anywhere else online, despite several searches. Either no one's done it or I'm not phrasing correctly.
Any help appreciated. :)
I'd advise against this. By doing it, you may annoy and alienate a portion of potential your users: for example my browser is set not to report referer information, others use anonymity networks. The dump bots you can catch by matching their reported user agent string (as seen here).
Otherwise it's simple: match against the HTTP_REFERER environmental variable in a RewriteCond:
RewriteCond %{HTTP_REFERER} ^$
RewriteRule .* http://example.com/
The RewriteCond checks to see if the referer is an empty string; the RewriteRule redirects everything to http://example.com/ root. This is a hard redirect, meaning that the server will issue an R=301 moved permanently header. If you just want to sneakily serve another resource, use a soft redirect by specifying a relative URL, like RewriteRule .* index.php. However, it may be kinder for people not reporting referrer information to redirect them to a page saying something like "You should enable referrer reporting if you want to read this page".
For more examples on such things, see the manual. There's a very similar prevent-hotlinking method there.

How to obtain a customized (and friendly) url using .htaccess?

Say I have a URL like http://abc.com/index.php?cat=37&subsubcat=0&subcat=199&page=product_detail&product_id=1661
Can I use .htaccess to redirect/rewrite this to a URL like http://abc.com/simplerName?
I found quite a few posts on SO that ask for friendly URLs, but I want to take it a step further, i.e. I'd like to specify what the subfolder name should be (in the above example, it's "simplerName"). Now I've got 10 URLs that I want to customize and I'm totally cool with specifying 10 rules for each URL.
But I'm not sure how to achieve this using .htaccess. Is this even possible?
Because you are using non-existing "subfiolders" to indicate which rule should match what, this is going to be pretty straight-forward:
You want something like this:
RewriteEngine On
RewriteRule ^/?category/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /index.php?cat=$1&subsubcat=$2&subcat=$3&page=$4&product_id=$5 [L]
RewriteRule ^/?something-else/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/ /index.php?something=$1&subsub=$2&sub=$3&page=$4&something_id=$5 [L]
etc.
You'll then need to change all the URLs that you serve to the friendly looking ones instead of the ones with the query string.

Problems with mod_rewrite and 301 redirects from one dynamic url to another

Hey all, I'm having all kinds of problems with a bunch of apache redirects just now and could really use some help!
I'm wating to put in a 301 redirect for a load of urls from a client's old site to their new site in the following format;
Old - page.php?pageNum_rs_all=0&totalRows_rs_all=112
New - page/sub?foo=bar
The values in the query sting for the old site don't in any way tie up to any ids or references on the new site, I only want to match that specific request and redirect to the new page.
It feels like I've tried just about every combination of rewriterule I can find online but still nothing seems to be working. This is running on Apache 2.2.
The rule I started with (and keep going back to) is;
RewriteRule ^/page.php\?pageNum_rs_all=0&totalRows_rs_all=112 /page/sub?foo=bar [R=301,L,NE]
Any help would be greatly appreciated!
c.
The reason that this doesn't work is that RewriteRule can't see the QueryString. To get at the Query String, you need to use RewriteCond. See http://wiki.apache.org/httpd/RewriteQueryString for examples of how this works.
I've always tended to use use a series of 301 redirects in the following manner
Redirect 301 /oldpage /newpage

How does URL rewriting work?

I am new to URL rewriting and I have an .htaccess file that looks like this:
RewriteEngine On
RewriteRule /*\.(css|js|gif|png|jpe?g)$ - [NC,L]
RewriteRule "^(.*)$" "www/index.php?_url=$1" [QSA,L]
Does this code just rewrite the code internally, or is it supposed to change to URL in the address bar? As of now it does not change the address bar, and I'm not really sure yet but I am thinking that I will probably want the option to do so for bookmarking purposes. So if there is a way could you please let me know or direct me to a pretty noob friendly guide on URL rewriting where I can figure it out on my own because I haven't been able to find one.
Thanks for the help!
As it stands, it will just do an internal rewrite. To redirect the user (thereby changing their address bar), add R to the flags (e.g. [NC,R,L] or [R,QSA,L])
URL rewriting is completely server-side (unless you do a redirect). The client (and thus their address bar) will not know what the server is doing with the URL.
Here's a good beginner tutorial that explains URL rewriting and goes through progressively more complex examples.