Score:1

Rewriterule not working with Joomla urls due to question mark and other special characters, also due to long urls

ma flag

So I moved from Joomla CMS and now I need to finish my permanent redirects, but the following redirect isn't working (it's actually returning an error 404):

RewriteCond %{HOST_NAME} =&?$
#RewriteRule ^component/adsmanager/?view=list&catid=0&rootid=0&Itemid=1000 /newurl [L,R=301]

I'm aware that this is happening because of the special characters like question mark, but I'm not really sure how to write the RewriteCond rule to make it work.

And there's also another URL throwing the same error:

RewriteRule ^blogold/12-notebooks-100-sale-promotion-buy-4000-now-get-it-today-more-text-goes-here-some-party-there right-thousands-of-dogs /store/buy-cheap-notebooks [L,R=301]

I used random words to post the url here in this question, but I kept the structure. Both redirects are throwing error 404, but I don't know the second one is throwing, I presume it's because the previous URL is too long.

These are all my .htaccess redirects:

#manter
RewriteEngine On
RewriteRule ^blog/emp/66-renda-extra-o-que-vender-na-faculdade /emp/o-que-vender [L,R=301]
RewriteRule ^blog/dm/105-resposta-ao-artigo-use-a-indiferenca-e-saiba-por-que-funciona-do-blog-autoridade-feminina /evoluir [L,R=301]
RewriteRule ^blog/emponline/cresca-com-a-gente-venha-escrever-conosco /institucional/colaborador [L,R=301]
RewriteRule ^cresca-com-a-gente-venha-escrever-conosco /institucional/colaborador [L,R=301]
RewriteRule ^emponline/empreendedorismo-online /emp-digital [L,R=301]
RewriteRule ^empreendedorismo-online /emp-digital [L,R=301]

#principal
RewriteRule ^blog/2-principal/131-bem-vindo /institucional/colaborador [L,R=301]

#Empreendedorismo
RewriteRule ^blog/emp/146-respondendo-estonia-01 /emp/sobre-a-estonia [L,R=301]
RewriteRule ^blog/emp/136-o-que-significa-uai /cultura/o-que-significa-uai [L,R=301]
RewriteRule ^blog/emp/134-classificados-lucrebem /emp/classificados-lucrebem [L,R=301]
RewriteRule ^blog/emp/128-freelancer-como-cobrar /emp/freelancer-como-cobrar [L,R=301]
RewriteRule ^blog/emp/125-nossos-servicos /nossos-servicos [L,R=301]
RewriteRule ^blog/emp/100-fazer-dinheiro /emp/fazer-dinheiro [L,R=301]
RewriteRule ^blog/emp/88-nao-seja-o-morto-vivo-mais-rico-da-sua-familia /evoluir/parentes-e-dinheiro [L,R=301]
RewriteRule ^blog/emp/85-perca-o-emprego-mas-nao-perca-a-dignidade /evoluir/dignidade-intacta [L,R=301]
RewriteRule ^blog/emp/86-anuncie-no-lucre-bem /emp/anuncie-no-lucrebem [L,R=301]
RewriteRule ^blog/emp/84-5-tecnicas-simples-e-altamente-eficientes-para-acabar-com-a-procrastinacao-de-uma-vez-por-todas /emp/tecnicas-anti-procrastinacao [L,R=301]
RewriteRule ^blog/emp/83-hora-de-procurar-um-emprego /emp/procurar-emprego [L,R=301]
RewriteRule ^blog/emp/82-7-maiores-pecados-mortais-que-as-escolas-e-faculdades-ensinam-e-que-irao-prejudica-lo-como-empreendedor-se-voce-deixar /emp/escolas-universidades-doutrinar [L,R=301]
RewriteRule ^blog/emp/78-extraindo-o-maximo-do-bomnegocio-com-e-aumentado-suas-vendas-em-ate-30x /emp/bomnegociocom [L,R=301]
RewriteRule ^blog/emp/74-como-lidar-com-americanos-estadunidenses-na-vida-pessoal-e-no-mundo-dos-negocios /emp/americanos-comportamento [L,R=301]
RewriteRule ^blog/emp/73-losango-tradicao-otimo-atendimento-e-solucoes-financeiras-para-diversos-tipos-de-perfis /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/emp/69-os-11-maiores-homens-de-sucesso-reais-e-ficticios-que-poderao-servir-como-licao-de-vida-para-voce /evoluir/homens-de-sucesso [L,R=301]
RewriteRule ^blog/emp/67-crie-seu-business-model-canvas-gratuitamente-on-the-fly /emp/business-model-canvas-gratis [L,R=301]
RewriteRule ^blog/emp/66-o-que-vender /emp/o-que-vender [L,R=301]
RewriteRule ^blog/emp/63-voce-realmente-se-importa-com-os-seus-clientes /emp/clientes-realmente-importam [L,R=301]
RewriteRule ^blog/emp/46-5-dicas-para-voce-preparar-seu-ambiente-para-trabalhar-em-casa /emp/ambiente-trabalhar-casa [L,R=301]
RewriteRule ^blog/conversor-de-moedas /emp/conversor-de-moedas [L,R=301]
RewriteRule ^blog/emp/43-sucesso-como-conseguir-em-1-ano-o-que-outros-nao-conseguem-com-menos-de-10-anos /emp/sucesso-1-ano [L,R=301]
RewriteRule ^blog/emp/32-como-tornar-se-um-homem-de-sorte /esp/homem-de-sorte [L,R=301]
RewriteRule ^blog/emp/58-5-etapas-pelas-quais-o-cliente-passa-antes-de-comprar-com-voce /emp/cliente-comprar [L,R=301]
RewriteRule ^blog/emp/30-volta-as-aulas-compre-da-china-e-economize-ate-90 /emp/material-escolar-china [L,R=301]
RewriteRule ^blog/emp/29-quantas-pessoas-ao-mesmo-tempo-voce-consegue-atender-teste-sua-velocidade-neste-jogo /games/jogo-de-lanchonete [L,R=301]
RewriteRule ^blog/emp/28-manual-anti-piramide-voce-nao-pode-passar-sem-isso /evoluir/manual-anti-piramide [L,R=301]
RewriteRule ^blog/emp/19-cachorro-quente-um-negocio-lucrativo /emp/cachorro-quente-lucrativo [L,R=301]

#emponline
RewriteRule ^blog/emponline/145-por-que-meus-anuncios-no-facebook-sao-exibidos-para-as-pessoas-erradas /emp-digital/anuncios-facebook-errados [L,R=301]
RewriteRule ^blog/emponline/143-nichos-wallace /emp-digital/nichos-de-mercado [L,R=301]
RewriteRule ^blog/emponline/142-background-animado-css3 /web/background-animado-css3 [L,R=301]
RewriteRule ^blog/emponline/132-por-que-voce-nao-ganha-dinheiro-na-internet /emp-digital/dinheiro-internet-motivo [L,R=301]
RewriteRule ^blog/emponline/127-tenha-seu-proprio-site /tenha-seu-proprio-site [L,R=301]
RewriteRule ^blog/emponline/117-nomade-digital /emp-digital/nomade-digital [L,R=301]
RewriteRule ^blog/emponline/111-a-melhor-hospedagem-de-sites-do-mundo /emp-digital/melhor-hospedagem-de-sites [L,R=301]
RewriteRule ^blog/emponline/104-ele-chutou-o-balde-no-brasil-para-ir-curtir-as-praias-da-tailandia-e-voce-vai-fazer-o-mesmo /emp-digital/curtindo-a-tailandia [L,R=301]
RewriteRule ^blog/emponline/92-ferramentas-seo /emp-digital/ferramentas-seo [L,R=301]
RewriteRule ^blog/emponline/90-o-hotmart-e-a-pirataria-uma-tragica-estoria-de-amor-em-3-atos /emp-digital/hotmart-pirataria [L,R=301]
RewriteRule ^blog/emponline/89-pontos-positivos-e-negativos-dos-melhores-programas-de-afiliados-que-possuem-ofertas-brasileiras /emp-digital/melhores-afiliados-brasileiros [L,R=301]
RewriteRule ^blog/emponline/87-classificados-redesocial /emp-digital/comunidade-lucrebem [L,R=301]
RewriteRule ^blog/emponline/81-duas-ferramentas-que-irao-aumentar-o-trafego-do-seu-site-assustadoramente-garantido /emp-digital/ferramentas-aumentar-trafego-site [L,R=301]
RewriteRule ^blog/emponline/80-vamos-fugir-deste-lugar-baby-facebook-capitalista /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/emponline/79-tres-ferramentas-uteis-para-donos-de-sites-e-afiliados /emp-digital/ferramentas-marketing-digital [L,R=301]
RewriteRule ^blog/emponline/77-do-fracasso-ao-sucesso-ele-chegou-ate-mesmo-a-desistir /emp-digital/do-fracasso-ao-sucesso [L,R=301]
RewriteRule ^blog/emponline/76-super-lista-de-4-milhoes-de-emails-brasileiros-para-mail-marketing /emp-digital/super-lista-para-mailmarketing [L,R=301]
RewriteRule ^blog/emponline/81-duas-ferramentas-que-irao-aumentar-o-trafego-do-seu-site-assustadoramente-garantido /emp-digital/ferramentas-aumentar-trafego-site [L,R=301]
RewriteRule ^blog/emponline/71-ja-nao-fazem-mais-programas-de-afiliados-cpa-como-antigamente /emp-digital/afiliados-como-antigamente [L,R=301]
RewriteRule ^blog/emponline/70-blog-de-nicho /emp-digital/blog-de-nicho [L,R=301]
RewriteRule ^blog/emponline/36-cresca-com-a-gente-venha-escrever-conosco /institucional/colaborador [L,R=301]
RewriteRule ^blog/emponline/49-torne-seu-vicio-do-facebook-em-dinheiro /emp-digital/vicio-facebook-dinheiro [L,R=301]
RewriteRule ^blog/emponline/62-pontos-negativos-do-hotmart-o-que-voce-precisa-saber /emp-digital/pontos-negativos-hotmart [L,R=301]
RewriteRule ^blog/emponline/59-mercado-promissor-o-seu-proximo-negocio-revender-hospedagem-de-sites /emp-digital/revenda-hospedagem-de-sites [L,R=301]
RewriteRule ^blog/emponline/55-ferramenta-para-encontrar-blogs-e-foruns-dofollow /emp-digital/ferramenta-encontrar-dofollow [L,R=301]
RewriteRule ^blog/emponline/52-sistema-de-loja-virtual-e-mais-de-280-mil-scripts-pra-voce-escolher /emp-digital/sistema-loja-virtual [L,R=301]
RewriteRule ^blog/emponline/51-tenha-seu-proprio-site-de-imoveis-sem-possuir-conhecimentos-tecnicos /emp-digital/site-de-imoveis [L,R=301]
RewriteRule ^blog/emponline/50-conheca-a-ana-paula-ela-faturou-5-digitos-com-apenas-2-horas-de-vendas-em-seu-blog /emp-digital/ana-paula-lucrou-5-digitos [L,R=301]
RewriteRule ^blog/emponline/48-editores-que-publicam-conteudo-ganham-68-da-receita-do-adsense-diz-google /emp-digital/editores-adsense-ganhos [L,R=301]
RewriteRule ^blog/emponline/45-quais-as-fontes-de-trafego-que-existem /emp-digital/fontes-trafego-existentes [L,R=301]
RewriteRule ^blog/emponline/25-por-que-eu-nao-consigo-fazer-dinheiro-com-meu-blog-a-pergunta-que-todo-blogueiro-se-faz-uma-vez-ou-outra /emp-digital/ganhar-dinheiro-blog [L,R=301]
RewriteRule ^blog/emponline/24-como-divulgar-seu-site-automagicamente-no-twitter-facebook-linkedin-e-conseguir-varias-visitas-rapidamente /emp-digital/divulgar-site-automagicamente [L,R=301]
RewriteRule ^blog/emponline/21-os-30-blogs-que-mais-fazem-dinheiro-na-internet /emp-digital/blogs-dinheiro-internet [L,R=301]
RewriteRule ^blog/emponline/20-ganhar-dinheiro-na-internet-ganhar-dinheiro-online-sem-gastar-um-tostao /emp-digital/ganhar-dinheiro-internet [L,R=301]
RewriteRule ^blog/emponline/42-negocio-na-internet-vs-negocio-fora-da-internet-qual-seria-o-melhor /emp-digital/negocio-internet-negocio-fisico [L,R=301]
RewriteRule ^blog/emponline/41-a-importancia-de-se-ter-uma-home-page-bem-elaborada /emp-digital/homepage-bem-elaborada [L,R=301]
RewriteRule ^blog/emponline/40-como-ganhar-dinheiro-com-seu-site-enquanto-voce-dorme /emp-digital/ganhar-dinheiro-dormindo [L,R=301]
RewriteRule ^blog/emponline/27-como-ganhar-dinheiro-com-o-adsense-da-maneira-correta /emp-digital/ganhar-dinheiro-adsense [L,R=301]
RewriteRule ^blog/emponline/26-como-importar-da-china-e-ganhar-dinheiro-revendendo-os-produtos-na-internet /emp-digital/como-importar-da-china [L,R=301]






#ebooks
RewriteRule ^blog/ebooks/11-o-poder-do-subconsciente /esp/poder-do-subconsciente [L,R=301]
RewriteRule ^recursos-gratis-marketing-e-publicidade/ebooks/11-o-poder-do-subconsciente.html /esp/o-poder-do-subconsciente [L,R=301]
RewriteRule ^blog/ebooks/10-a-arte-da-guerra-por-sun-tzu-versao-do-james-clavell /ebooks/arte-da-guerra-negocios [L,R=301]
RewriteRule ^blog/ebooks/9-pai-rico-pai-pobre /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/ebooks/8-1001-maneiras-de-enriquecer /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/ebooks/7-o-monge-e-o-executivo /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/ebooks/4-crie-1000-backlinks-de-altissimo-pagerank-utilizando-sites-de-web-2-0-ebook-em-ingles /emp-digital/ferramentas-seo [L,R=301]
RewriteRule ^blog/ebooks/1-como-vender-qualquer-coisa-a-qualquer-um /emp/o-que-vender [L,R=301]

#desenvolvimento pessoal
RewriteRule ^blog/evoluir/133-17-tecnicas-excelentes-para-se-manter-motivado-todos-os-dias /evoluir/17-tecnicas-excelentes-para-se-manter-motivado-todos-os-dias [L,R=301]
RewriteRule ^blog/evoluir/124-compressao-do-tempo /evoluir/compressao-do-tempo [L,R=301]
RewriteRule ^blog/evoluir/121-o-lado-besta-dos-signos /esp/lado-besta-dos-signos [L,R=301]
RewriteRule ^blog/evoluir/110-quantos-ismos-mais /sem-categoria/quantos-ismos-mais [L,R=301]
RewriteRule ^blog/evoluir/108-depressao-o-que-e-de-onde-vem-e-para-onde-vai /evoluir/o-que-e-depressao [L,R=301]

#games
RewriteRule ^blog/games/106-for-honor-graficos-comparacao-ps4-pc-xbox-one-beta /va/blog-de-nicho [L,R=301]

#Espiritualidade
RewriteRule ^blog/evoluir/101-a-verdade-sobre-a-seicho-no-ie-verdade /esp/verdade-sobre-seichonoie [L,R=301]

#cresça conosco
RewriteRule ^cresca-conosco /institucional/colaborador [L,R=301]
RewriteRule ^contate-nos /contato [L,R=301]

#anunciar no site
#         /component/adsmanager/?view=list&catid=0&rootid=0&Itemid=1097
RewriteCond %{QUERY_STRING} =view=list&catid=0&rootid=0&Itemid=1097 [NC]
RewriteRule ^component/adsmanager/$ /emp-digital/comunidade-lucrebem [NC,QSD,R=301,L]



##
RewriteRule ^blog/emponline/75-envie-12000-emails-para-ate-2000-contatos-gratuitamente-o-email-marketing-que-voce-tanto-procura-esta-aqui/$ /emp-digital/envie-milhares-de-emails [L,R=301]




#Outros links
RewriteRule ^va/bomnegocio /emp-digital/blog-de-nicho [L,R=301]
RewriteRule ^va/site-para-corretor-de-imoveis /emp-digital/tenha-seu-proprio-site [L,R=301]


#todas as categorias
RewriteRule ^blog/2-principal / [L,R=301]
RewriteRule ^blog/ebooks /ebooks [L,R=301]
RewriteRule ^blog/downloads /downloads [L,R=301]
RewriteRule ^blog/12-artigos$ /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/13-cursos /cursos [L,R=301]
RewriteRule ^blog/emponline /emp-digital [L,R=301]
RewriteRule ^blog/emp /emp [L,R=301]
RewriteRule ^blog/20-newsletters /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/21-uncategorized /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/dm /evoluir [L,R=301]
RewriteRule ^blog/politica /va/blog-de-nicho [L,R=301]
RewriteRule ^blog/games /games [L,R=301]
RewriteRule ^blog/evoluir /evoluir [L,R=301]



# RewriteRule ^blog/dm/112-americanas-procuram-brasileiros [L,R=301] #
# RewriteRule ^blog/dm/113-como-encontrar-o-perfil-de-uma-mulher-do-badoo [L,R=301] #
in flag
Do the 404 errors happen before it after the redirect? Please use a command line client like wget or curl for testing and edit the output into the question.
Brave Warrior avatar
ma flag
It doesn't redirect the URL at all.
kz flag
Please include the URLs you are wanting to redirect from/to, rather than just posting the directive and saying that it’s not working. Both directives are clearly wrong, but are you really wanting to preserve the query string in first rule? And the 2nd rule would result in a 500 Error, not a 404 as you’ve stated?
kz flag
What “other” special characters are you referring to? There’s no other special characters in the URL in the first directive you posted. The first question mark `?` delimits the query string. Providing you are using it for this purpose then there is no “problem”. Confusion might come about from using “special characters” as literal characters in other parts of the URL.
Score:1
kz flag

To 301 redirect /component/adsmanager/?view=list&catid=0&rootid=0&Itemid=1000 to /newurl you would use the following mod_rewrite directive near the top of your .htaccess file:

RewriteCond %{QUERY_STRING} =view=list&catid=0&rootid=0&Itemid=1000 [NC]
RewriteRule ^component/adsmanager/$ /newurl [NC,QSD,R=301,L]
RewriteRule ^blogold/12-notebooks-100-sale-promotion-buy-4000-now-get-it-today-more-text-goes-here-some-party-there right-thousands-of-dogs /store/buy-cheap-notebooks [L,R=301]

This directive has an erroneous space in the middle of the regex, so this would actually result in a 500 Internal Server Error ("bad flags delimiter") if it did match. Otherwise, there's nothing particularly wrong with this directive, depending on the URL you are trying to match. You have not reached any limitation of "URL length" here. Any limitation is due to the underlying filesystem, not Apache per se. If the last path segment on the URL is >257 chars then you will potentially have problems trying to match this URL in .htaccess.

The order of these directives in .htaccess is important. If you are using WordPress then these redirects need to go before the WordPress front-controller (ie. before the # BEGIN WordPress section).

NB: I'm assuming, from the format of your regex (and talk of long URL issues) that you are using these directives in .htaccess (although you've not explicitly stated this). If, however, you are using these directives in a server or virtualhost context (which is usually assumed on ServerFault) then you'll need to match the slash prefix on the URL-path. Using these directives in a server context would also workaround any URL-path limitation associated with the underlying filesystem.

Brave Warrior avatar
ma flag
`RewriteCond %{QUERY_STRING} =view=list&catid=0&rootid=0&Itemid=1000 [NC] RewriteRule ^component/adsmanager/$ /newurl [NC,QSD,R=301,L]` Is not working. It is redirecting including the `=view=list&catid=0&rootid=0&Itemid=1000` in front of the rest.
Brave Warrior avatar
ma flag
Also `RewriteRule ^blogold/12-notebooks-100-sale-promotion-buy-4000-now-get-it-today-more-text-goes-here-some-party-there right-thousands-of-dogs /store/buy-cheap-notebooks [L,R=301]` has a typo, it was supposed to have a hyphen there.
kz flag
@BraveWarrior "including the `=view...` in front of the rest" - What do you mean by that exactly? This directive specifically _removes_ the query string (that's what the `QSD` flag does - Query String Discard) so there should be no mention of the query string in the redirected URL. Maybe there is a conflict with other directives? Or perhaps you are seeing a cached response? If you have previously experimented with 301s then these are cached persistently by the browser. You need to ensure the browser cache is cleared and test with 302s to avoid potential caching issues.
kz flag
@BraveWarrior Note that the `=` prefix on the _CondPattern_ `=view...` is part of the directive. It is a prefix-operator that changes the type of comparison the directive performs. The `=` operator makes it an exact string comparison (rather than a regex), so for this `=` prefix to appear anywhere in the redirected URL doesn't really appear to make sense.
kz flag
@BraveWarrior "has a typo, it was supposed to have a hyphen there" - in that case, there is no problem with the directive itself - depending on the URL you are trying to match. There are no "URL length" restrictions associated with the example you posted. Again, maybe there is a conflict with other directives? Please edit your question to include the contents of your `.htaccess` file, with these directives in place. (I assume you are using a `.htaccess` file?) Do you have any other `.htaccess` files along the filesystem path?
Brave Warrior avatar
ma flag
I'll include all my redirects in the question.
kz flag
@BraveWarrior There doesn't appear to be any conflicts in what you've posted, however, that's not your complete `.htaccess` file - is it? You presumably have other rewrites that route your URLs to a front-controller - any conflict is more likely to be with these other directives. Please include your _complete_ `.htaccess` file in your question. Thanks.
kz flag
To clarify, all the other redirects appear to work OK?
Brave Warrior avatar
ma flag
I didn't test them all, but all the ones that I've tested are working just fine, I only found a problem with those two that I've shared first. The first one had a typo, I fixed the typo, but now it redirects to somewhere else and I not even know why, I tried to see if there was a duplicated redirect, but there wasn't, I also checked the article where it redirects to, the article is fine too (no redirect code in article's page), I even checked if there was another url related to the article, among the redirects.
Brave Warrior avatar
ma flag
also checked the article where it's pointed to*
kz flag
@BraveWarrior "Please include your _complete_ `.htaccess` file in your question." - Are you saying this is your complete `.htaccess` file? "including the `=view...` in front of the rest" - What did you mean by that in your earlier comment? "The first one had a typo, I fixed the typo" - I thought you said earlier it was the _second_ one that had the typo (a _space_ where it should have been a _hyphen_)? "but now it redirects to somewhere else" - which one redirects to where?
Brave Warrior avatar
ma flag
Never mind about this one: /component/adsmanager/?view=list&catid=0&rootid=0&Itemid=1097 It started working. It was probably a cache problem. Yes, the one with a typo is the pretty long one, it it is redirecting to a category, I don't know why. By the way, should I change the other redirects structure or leave them as they? I ask because they're working.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.