How do I write less RewriteRules? - .htaccess

Years ago I installed link building scrips in multiple sub directories on my site. Spiders picked picked up the addresses and to this day they show up.
I can redirect them all with 13 different RewriteRules but I need do do this for 13 subdirectories. Is there a way to accomplish this without 169 lines of code?
RewriteRule ^adult/join/ http://www.domain/adult/ [R=301,L]
RewriteRule ^adult/stats/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/details/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/overall-stats/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/lost_pw/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/review/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/reviews/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/rank/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/search/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/category/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/user_cpl/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/page/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^adult/app/ http://www.domain.com/adult/ [R=301,L]
RewriteRule ^teen/join/ http://www.domain/teen/ [R=301,L]
RewriteRule ^teen/stats/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/details/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/overall-stats/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/lost_pw/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/review/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/reviews/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/rank/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/search/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/category/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/user_cpl/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/page/ http://www.domain.com/teen/ [R=301,L]
RewriteRule ^teen/app/ http://www.domain.com/teen/ [R=301,L]
And then for ^gay ^gayteen ^senior ect.

You can use:
RewriteRule ^(adult|teen|gay|gayteen|senior)/(?:join|stats|details|overall-stats|lost_pw|reviews?|rank|search|category|user_cpl|page|app)/? http://www.domain.com/$1/ [NC,R=301,L]
Only one line ;-)
EDIT: with domain option:
RewriteRule ^((?:adult|teen|gay|gayteen|senior)/)?(?:join|stats|details|overall-stats|lost_pw|reviews?|rank|search|category|user_cpl|page|app)/? http://www.domain.com/$1 [NC,R=301,L]

Related

htaccess - how to redirect /q/<id> to /#/q/<id>?

Here is my Rewrite Rule :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule "^/q/(a-zA-Z0-9)*/?$" "/#/q/$1"
Something does not work, I don't understand what.
I want to redirect /q/xxx to /#/q/xxx
How can I do it ?
here is the solution: RewriteRule ^q/([a-zA-Z0-9-]+)/?$ /#/q/$1 [L,NC,R=301,NE]
The flags were missing

creating a htaccess redirect that includes a special character

I need to create a 301 redirect within my htaccess file, the problem is the links have the (?) special character:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^news/news-item?blue-spares-appoints-stuart-truckel-as-sales-director?$ http://www.example.com/news [R=301,L]
RewriteRule ^news/news-item?hugely-successful-scot-plant-for-blue-scotland?$ http://www.example.com/news [R=301,L]
RewriteRule ^news/news-item?port-of-workington-takes-specialist-fuchs-materials-handler?$ http://www.example.com/news [R=301,L]
RewriteRule ^news/news-item?blue-spares-go-green-with-new-shredder?$ http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/shredders/used-machinery-item?000739:beast-bandit-2860?$ http://www.example.com/machinery [R=301,L]
RewriteRule ^about-us/careers/careers-item?project-manager-blue-machinery-london-ltd http://www.example.com/about [R=301,L]
RewriteRule ^news/news-item?thompsons-plant-hire-continues-allegiance-to-doppstadt-and-blue-machinery http:// http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/finlay/used-machinery-item?000549:798-double-deck-skin-trommel-screen http://www.example.com/machinery [R=301,L]
RewriteRule ^used-machinery/en/screeners/used-machinery-item?000549:798-double-deck-skin-trommel-screen http://www.example.com/machinery [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/ http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?1 http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?4 http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?3 http://www.example.com/product-videos [R=301,L]
RewriteRule ^product-videos/shredders/doppstadt-ak-range/?7 http://www.example.com/product-videos [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
What would be the best way to redirect each of these urls to their correct destinations?
All characters after ? sign is part of QUERY_STRING, you must add conditions to query string before rules:
RewriteCond %{QUERY_STRING} ^[0-9a-z\-\:]+$
RewriteRule ^product-videos/shredders/doppstadt-ak-range/? http://www.example.com/product-videos [R=301,L]
RewriteRule ^news/news-item http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/shredders/used-machinery-item$ http://www.example.com/machinery [R=301,L]
RewriteRule ^about-us/careers/careers-item$ http://www.example.com/about [R=301,L]
RewriteRule ^news/news-item$ http:// http://www.example.com/news [R=301,L]
RewriteRule ^used-machinery/en/finlay/used-machinery-item http://www.example.com/machinery [R=301,L]
RewriteRule ^used-machinery/en/screeners/used-machinery-item http://www.example.com/machinery [R=301,L]
The adobe rules will match:
news/news-item?blue-spares-appoints-stuart-truckel-as-sales-director
news/news-item?hugely-successful-scot-plant-for-blue-scotland
news/news-item?port-of-workington-takes-specialist-fuchs-materials-handler
news/news-item?blue-spares-go-green-with-new-shredder
product-videos/shredders/doppstadt-ak-range/?1
product-videos/shredders/doppstadt-ak-range/?4
product-videos/shredders/doppstadt-ak-range/?3
product-videos/shredders/doppstadt-ak-range/?7
And other URLs
See RewriteCond documentation:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#RewriteCond

Make redirect from www to non-www and allow futher processing

I want to redirect site from www to non-www.
The main issue is that .htaccess file of CMS is very complicated.
Otherwise this instruction in the start of the .htaccess should have worked
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.ecotrans-spb.ru$ [NC]
RewriteRule ^(.*)$ http://ecotrans-spb.ru/$1 [R=301,L]
What flags should I set here instead of these to allow .htaccess process the URL further?
Generic CMS .htaccess
AddDefaultCharset UTF-8
ErrorDocument 404 /404
ErrorDocument 401 /password.php
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.ecotrans-spb.ru$ [NC]
RewriteRule ^(.*)$ http://ecotrans-spb.ru/$1 [R=301]
# Админка теперь по адресу /simpla
RewriteRule ^admin/?$ simpla [L]
# Каталог товаров
RewriteRule ^catalog/([^/]+)/?$ index.php?module=ProductsView&category=$1 [L,QSA]
RewriteRule ^catalog/([^/]+)/([^/]+)/?$ index.php?module=ProductsView&category=$1&brand=$2 [L,QSA]
RewriteRule ^products/([^/]+)/?$ index.php?module=ProductView&product_url=$1 [L,QSA]
RewriteRule ^products/?$ index.php?module=ProductsView [L,QSA]
RewriteRule ^brands/([^/]+)/?$ index.php?module=ProductsView&brand=$1 [L,QSA]
RewriteRule ^brands/([^/]+)/page_([^/]+)/?$ index.php?module=ProductsView&brand=$1&page=$2 [L,QSA]
# Поиск товаров
RewriteRule ^search/([^/]+)/?$ index.php?module=ProductsView&keyword=$1 [L,QSA]
RewriteRule ^search/?$ index.php?module=ProductsView [L,QSA]
# Блог
RewriteRule ^blog/([^/]+)/?$ index.php?module=BlogView&url=$1 [L,QSA]
RewriteRule ^blog/?$ index.php?module=BlogView [L,QSA]
# Корзина и заказы
RewriteRule ^cart/?$ index.php?module=CartView [L,QSA]
RewriteRule ^cart/([^/]+)/?$ index.php?module=CartView&add_variant=$1 [L,QSA]
RewriteRule ^cart/remove/([^/]+)/?$ index.php?module=CartView&delete_variant=$1 [L,QSA]
RewriteRule ^order/([^/]+)/?$ index.php?module=OrderView&url=$1 [L,QSA]
RewriteRule ^order/?$ index.php?module=OrderView [L,QSA]
# Для пользователей
RewriteRule ^user/login/?$ index.php?module=LoginView [L,QSA]
RewriteRule ^user/register/?$ index.php?module=RegisterView [L,QSA]
RewriteRule ^user/logout/?$ index.php?module=LoginView&action=logout [L,QSA]
RewriteRule ^user/password_remind/?$ index.php?module=LoginView&action=password_remind [L,QSA]
RewriteRule ^user/password_remind/([0-9a-z]+)/?$ index.php?module=LoginView&action=password_remind&code=$1 [L,QSA]
RewriteRule ^user/?$ index.php?module=UserView [L,QSA]
# Google sitemap
RewriteRule ^sitemap.xml?$ sitemap.php [L,QSA]
# XML
RewriteRule ^yandex.xml?$ yandex.php [L,QSA]
# feedback
RewriteRule ^contact/?$ index.php?module=FeedbackView [L,QSA]
#downloads
RewriteRule ^order/([^/]+)/([^/]+)/?$ index.php?module=OrderView&url=$1&file=$2 [L,QSA]
# Статические страницы
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ index.php?module=PageView&page_url=$1 [L,QSA]
RewriteRule ^/?$ index.php?module=MainView&page_url= [L,QSA]
# Ресайз картинок на лету
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^files/products/(.+) resize/resize.php?file=$1&token=%{QUERY_STRING}
#RewriteCond %{THE_REQUEST} ^GET\ (.*)files/products/(.*)\?([A-z0-9]*)
#RewriteRule ^files/products/(.+) resize/resize.php?file=%2&token=%3 [L,NE]
Removing the L from the last line will allow it to continue.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.ecotrans-spb.ru$ [NC]
RewriteRule ^(.*)$ http://ecotrans-spb.ru/$1 [R=301]
[L] is a flag that, if this rule matches, then no other rule matching will be performed.

Issue with .htaccess file for R301 redirections

Good morning,
I changed the architecture of my website www.sirima.net, and I would like to setup R301 redirections from the old structure to the new structure, thanks to an .htaccess file.
Unfortunately, the .htaccess file below does not work. :-(
Thanks in advance for your help.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^/une_partie_d_elle.html http://www.sirima.net/ [R=301,L]
RewriteRule ^/bio/bio2.html http://www.sirima.net/biographie/ [R=301,L]
RewriteRule ^/bio/bio3.html http://www.sirima.net/biographie/ [R=301,L]
RewriteRule ^/bio/bio4.html http://www.sirima.net/biographie/ [R=301,L]
RewriteRule ^/bio/bio5.html http://www.sirima.net/biographie/ [R=301,L]
RewriteRule ^/bio/bio6.html http://www.sirima.net/biographie/ [R=301,L]
RewriteRule ^/bio/index.html http://www.sirima.net/biographie/ [R=301,L]
RewriteRule ^/disco/credits.html http://www.sirima.net/discographie/credits.php [R=301,L]
RewriteRule ^/disco/index.html http://www.sirima.net/discographie/ [R=301,L]
RewriteRule ^/disco/sirima01.html http://www.sirima.net/discographie/01-sometimes-love-i sn-t-enough.php [R=301,L]
RewriteRule ^/disco/sirima02.html http://www.sirima.net/discographie/02-no-reason-no-rhyme.php [R=301,L]
RewriteRule ^/disco/sirima03.html http://www.sirima.net/discographie/03-his-way-of-loving-me.php [R=301,L]
RewriteRule ^/disco/sirima04.html http://www.sirima.net/discographie/04-kym.php [R=301,L]
RewriteRule ^/disco/sirima05.html http://www.sirima.net/discographie/05-a-part-of-me.php [R=301,L]
RewriteRule ^/disco/sirima06.html http://www.sirima.net/discographie/06-i-need-to-know.php [R=301,L]
RewriteRule ^/disco/sirima07.html http://www.sirima.net/discographie/07-ticket-to-the-moon.php [R=301,L]
RewriteRule ^/disco/sirima08.html http://www.sirima.net/discographie/08-i-believed.php [R=301,L]
RewriteRule ^/disco/sirima09.html http://www.sirima.net/discographie/09-daddy.php [R=301,L]
RewriteRule ^/disco/sirima10.html http://www.sirima.net/discographie/10-was-it-a-dream.php [R=301,L]
RewriteRule ^/hommage.html http://www.sirima.net/raphael-torr-ballade-pour-sirima.php [R=301,L]
RewriteRule ^/interviews/0003xx01.txt http://www.sirima.net/interviews/2000-03-la-bas-secrets-de-chansons-mars-2000.php [R=301,L]
RewriteRule ^/interviews/8711xx01.txt http://www.sirima.net/interviews/1987-11-ok-magazine-620-novembre-1987.php [R=301,L]
RewriteRule ^/interviews/880404.txt http://www.sirima.net/interviews/1988-04-top-50-109-4-10-avril-1988.php [R=301,L]
RewriteRule ^/interviews/891213.txt http://www.sirima.net/interviews/1989-12-ici-paris-2319-13-au-19-decembre-1989.php [R=301,L]
RewriteRule ^/interviews/8912xx01.txt http://www.sirima.net/interviews/1989-12-a-part-of-me-coffret-promo.php [R=301,L]
RewriteRule ^/interviews/9001xx01.txt http://www.sirima.net/interviews/1990-01a-paroles-et-musiques-25-janvier-1990.php [R=301,L]
RewriteRule ^/interviews/9001xx02.txt http://www.sirima.net/interviews/1990-01b-paroles-et-musiques-25-janvier-1990.php [R=301,L]
RewriteRule ^/interviews/index.html http://www.sirima.net/interviews/ [R=301,L]
RewriteRule ^/menu.html http://www.sirima.net/ [R=301,L]
RewriteRule ^/menu_texte.html http://www.sirima.net/ [R=301,L]
RewriteRule ^/merci.html http://www.sirima.net/merci.php [R=301,L]
RewriteRule ^/photos/index.html http://www.sirima.net/photos/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.sirima\.net [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.sirima.net/$1 [L,R,NE]

.htaccess not ignoring a directory when applying rules

Stuck on this one for a while before I turned for help, any assistance is appreciated.
For all intensive purposes I had to use this method to redirect all the lowercase URLS to work on my current setup, its not pretty I know.
The issue is when I use this setup all the Urls in the directory its supposed to skip break. For example /dont-touch/File1 stops working - I need to insert the below code so the lowercase rewrites dont apply to /dont-touch
RewriteCond %{REQUEST_URI} "/dont-touch/" [OR]
RewriteCond %{REQUEST_URI} "/blog/"
RewriteRule ![A-Z] - [S=26]
RewriteRule ^(.*)A(.*)$ /$1a$2 [R,L]
RewriteRule ^(.*)B(.*)$ /$1b$2 [R,L]
RewriteRule ^(.*)C(.*)$ /$1c$2 [R,L]
RewriteRule ^(.*)D(.*)$ /$1d$2 [R,L]
RewriteRule ^(.*)E(.*)$ /$1e$2 [R,L]
RewriteRule ^(.*)F(.*)$ /$1f$2 [R,L]
RewriteRule ^(.*)G(.*)$ /$1g$2 [R,L]
RewriteRule ^(.*)H(.*)$ /$1h$2 [R,L]
RewriteRule ^(.*)I(.*)$ /$1i$2 [R,L]
RewriteRule ^(.*)J(.*)$ /$1j$2 [R,L]
RewriteRule ^(.*)K(.*)$ /$1k$2 [R,L]
RewriteRule ^(.*)L(.*)$ /$1l$2 [R,L]
RewriteRule ^(.*)M(.*)$ /$1m$2 [R,L]
RewriteRule ^(.*)N(.*)$ /$1n$2 [R,L]
RewriteRule ^(.*)O(.*)$ /$1o$2 [R,L]
RewriteRule ^(.*)P(.*)$ /$1p$2 [R,L]
RewriteRule ^(.*)Q(.*)$ /$1q$2 [R,L]
RewriteRule ^(.*)R(.*)$ /$1r$2 [R,L]
RewriteRule ^(.*)S(.*)$ /$1s$2 [R,L]
RewriteRule ^(.*)T(.*)$ /$1t$2 [R,L]
RewriteRule ^(.*)U(.*)$ /$1u$2 [R,L]
RewriteRule ^(.*)V(.*)$ /$1v$2 [R,L]
RewriteRule ^(.*)W(.*)$ /$1w$2 [R,L]
RewriteRule ^(.*)X(.*)$ /$1x$2 [R,L]
RewriteRule ^(.*)Y(.*)$ /$1y$2 [R,L]
RewriteRule ^(.*)Z(.*)$ /$1z$2 [R,L]
This is how my HTACCESS file looks, Ive tried putting it at the start... the end. Nothing works.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !/component/
RewriteCond %{REQUEST_URI} !/administrator/
RewriteCond %{HTTP_HOST} ^(www.)?example\.com$ [NC]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
First, you're better of using RewriteMap here:
RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]
This will change everything in the URI to lowercase, instead of 26 slow rules.
Secondly, for the directories not to be rewritten, I don't think you should put them in quotes
RewriteCond %{REQUEST_URI} ^/dont-touch(/|$) [OR]
RewriteCond %{REQUEST_URI} ^/blog(/|$)
RewriteRule ^.*$ - [NC,L]
And if you put the [L] flag at the end, nothing below that point will interfere with this rule.

Resources