Redirect with Htaccess - Calendar Pagination - regex

my calendar pagination like this
example.com/calendar/2014/page/2
example.com/calendar/2014/04/page/2
example.com/calendar/2014/04/05/page/2
someone go to /page/ without last number i want to redirect for without /page/ ... i want like this type.. if can someone help me for this one.
Note : i want 301 redirect
i want to redirect like this
example.com/calendar/2014/page/ (with slash or without slash) -> example.com/calendar/2014
example.com/calendar/2014/04/page/ (with slash or without slash) -> example.com/calendar/2014/04
example.com/calendar/2014/04/05/page/ (with slash or without slash) -> example.com/calendar/2014/04/05
here is my code htaccess
/calendar/.htaccess
Options +FollowSymLinks
RewriteEngine On
RewriteBase /calendar/
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteRule ^([0-9]+)/?$ load.php?year=$1 [L,QSA]
RewriteRule ^([0-9]+)/(page)/([0-9]+)/?$ load.php?year=$1&p=$3 [L,QSA]
RewriteRule ^([0-9]+)/([0-9]+)/?$ load.php?year=$1&month=$2 [L,QSA]
RewriteRule ^([0-9]+)/([0-9]+)/(page)/([0-9]+)/?$ load.php?year=$1&month=$2&p=$4 [L,QSA]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/?$ load.php?year=$1&month=$2&date=$3 [L,QSA]
RewriteRule ^([0-9]+)/([0-9]+)/([0-9]+)/(page)/([0-9]+)/?$ load.php?year=$1&month=$2&date=$3&p=$5 [L,QSA]

Insert this 301 rule just below your existing 1st www enforcing 301 rule:
RewriteCond %{THE_REQUEST} \s/+(.+?)/page/?[\s?] [NC]
RewriteRule ^ /%1? [R=301,L]

Related

How i can stop processing of htaccess?

I've got two directives in my htaccess.
RewriteRule ^(it|en|de)/(.*) $2?lng=$1 [L,QSA]
That means: for all url with start with it, en, de set url variables "lng" to $lang
Now, i want that all pages that didn't start with a language code to be redirected to /it:
I try this:
RewriteCond %{REQUEST_URI} !^/(it|en|de)/{0,1}(.*)
RewriteRule ^(.*)$ /it/$1 [R=301,L]
but when i call:
http://HOST/my-page
on url, i get, with a "ERR_TOO_MANY_REDIRECTS":
http://HOST/it/my-page?lng=it&lng=it&{many-others-lng=it}
the word "/it" is added correctly at the beginning of the url, but also infinity "lng=it"
I use "L" flag: this shoud stop processing the rule set.
Any hint?
EDIT:
Add my full .htaccess
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(it|en|de)/(.*) $2?lng=$1 [L,QSA]
RewriteRule ^catalogue-men$ views/main/pages/index.cfm?gen=1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/it/
RewriteRule ^(.*)$ /it/$1 [R=301,L]
Just remember that mod_rewrite runs in loop until there is no rule that fires. You are getting redirect loop because first rule is removing /it/ from start of URI and 301 rule is adding it in front.
Reordering your rules
Using THE_REQUEST instead of REQUEST_URI
Add /catalogue-men in your skip condition
This .htaccess should work for you:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} !\s/+(?:it|catalogue-men)/ [NC]
RewriteRule ^(.*)$ /it/$1 [R=301,L,NE]
RewriteRule ^(it|en|de)/(.*) $2?lng=$1 [L,QSA]
RewriteRule ^catalogue-men$ views/main/pages/index.cfm?gen=1 [QSA,NC,L]

New .htaccess puzzle

I have this link:
index.php/forums/viewforum/5/
Now, I want that "forums" word in URL to become dynamic such that which ever word I replace it with, it still redirects to the same URL.
For example, if I have:
ProductA/viewforum/5/
it redirects to:
forums/viewforum/5/
For example, if I have:
ProductB/viewforum/13/
it redirects to:
forums/viewforum/13/
In other words, if there's a "view forum" word in the URL, it should trigger this rewrite.
I already have a .htaccess that removes the index.php from the URL so the rewrite rule should consider that too.
Is it possible?
HTACCESS:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteEngine on
RewriteCond %{REQUEST_URI} (member)
RewriteRule ^(.*)$ http://%{SERVER_NAME}/404 [R=301,L]
RewriteRule ^[.*]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
RewriteCond $1 !^(images|favicon\.ico|robots\.txt|index.php) [NC]
RewriteRule ^(.*)$ /home/ddco/public_html/index.php/$1? [L]
</IfModule>
You can use this rewrite rule:
RewriteCond %{THE_REQUEST} /forums/ [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^[^/]+/(viewforum/[0-9]+/?)$ /forums/$1 [L,NC]
Also suggesting you to post your current .htaccess rules as well for further investigation.

Rewriterule with redirect 301 and regexp not working

I have got few issues with redirect 301 and rewriterule in htaccess.
I've got few thousands subpages with new links, which have to be redirected permamently, but I can manage it with 10-20 rewriterules. But not everything is going as it should be...
Basic rewriterule works just fine, example:
RewriteRule ^subpage-([0-9]+)*\.html$ subpage.php?p=$1 [L]
It gets me from subpage-1.html to subpage.php?p=1 (no 301 needed here, it's just example, rewriterule does it's job).
Simple rewriterule with 301 redirect also works fine:
RewriteRule ^subfolder/subpage.php$ /subpage.html [L,R=301]
Althrough I don't know why I have to put "/" before "new-subpage". If I don't I'm being redirected to "domain/whole-ftp-path/new-subpage.html" and not "domain/new-subpage.html". Is it just redirect 301 thing?
And the main event:
RewriteRule ^subfolder/subpage.php?p=([0-9]+)$ /subpage-$1.html [L,R=301]
This does nothing, I'm getting domain/subfolder/subpage.php?p=1 with 404 (old subpages does not exist anymore in the same location).
What I am doing wrong?
You cannot match QUERY_STRING in RewriteRule.
Replace your .htaccess with this:
RewriteCond %{THE_REQUEST} \s/+subfolder/oldsubpage\.php\?f=([^\s&]+) [NC]
RewriteRule ^ /newsubpage-%1.html? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+subfolder/oldsubpage\.php[\s/] [NC]
RewriteRule ^ /newsubpage.html? [R=302,L]
RewriteRule ^subpage-([0-9]+)*\.html$ /subpage.php?p=$1 [L,NC,QSA]
RewriteRule ^newsubpag\.html$ /newsubpage.php [L,NC]
Has to do with the query string. If you have only one subfolder you could try this:
RewriteCond %{QUERY_STRING} p=([0-9]+)
RewriteRule .*$ /subpage-%1.html [L,R=301]
With more subfolders you could try:
RewriteRule ^subfolder/subpage.php(.*)$ /subpage$1 [QSA]
RewriteCond %{QUERY_STRING} p=([0-9]+)
RewriteRule ^(.*)$ /$1-%1.html [L,R=301]

How to handle both /url and /url/ redirects with htaccess?

I am rewriting all URLs and shorthand some of them:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(([^\.]+))\.pl [NC]
RewriteRule ^(.*)$ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test$ test-page.php [R=301,L]
so that
example.pl/test redirects to example.pl/test-page.php
but
example.pl/test/ redirects to example.pl/
while it should:
example.pl/test/ redirects to example.pl/test-page.php
How to handle both /url and /url/ redirects?
I assume that it has to something with trailing slash.
You need to make trailing slash optional using /?$ regex. Try these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.pl [NC]
RewriteRule ^ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test/?$ test-page.php [NC,R=301,L]
Make sure to test this in a new browser to avoid 301 caching issues.
Try this:
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
RewriteRule ^test$ test-page.php [R=301,L]
Tested here.

Handle query parameters recursively using htaccess

I am here again with the query of re-writing URL :(.
I am using htaccess for re-writing, below is my htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]
Before asking my queries let me give you short explanation about how above code is working. I have a URL like http://www.mysite.com/ (for index) and other pages could be executed by using a parameter called file_name=page_name like http://www.mysite.com/?file_name=my_blog. My htaccess is helping me to exclude the file_name parameter and make a new URL like http://www.mysite.com/my_blog/.
Now I do have a query:
Right now there is already a parameter file_name and now I want to give extra parameters like http://www.mysite.com/?file_name=my_blog&blog_alias=welcome-to-new-generation and wanted this to look like http://www.mysite.com/my_blog/welcome-to-new-generation/. So if I add any number of parameter there parameter name should be removed and only parameter value comes with slash.
Please note: Parameter name could be anything.
Please please please help.
This is a very interesting problem that requires recursive implementation of mod_rewrite rules. Put this code in your .htaccess:
Update II As per further comments, redirects /?file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v URI to /file_name+my_blog/blog_alias+welcome-to-new-generation/foo+bar/n+v
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# forwards ?file_name=my_blog&blog_alias=welcome-to-new-generation to
# /file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?([^\s]+)\s [NC]
RewriteRule ^$ %1? [L]
# redirects /file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v
# to /file_name+my_blog/blog_alias+welcome-to-new-generation/fo+barn+/v
RewriteRule ^(.*/)?([^=]+)=([^&]+)&(.*)$ $1$2+$3/$4 [L]
RewriteRule ^(.*/)?([^=]+)=(.*)$ $1$2+$3 [L,R]
# internal forward from /file_name+my_blog/blog_alias+welcome-to-new-generation to
# /?file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteRule ^([^\+]+)\+([^/]+)/(.*)$ $3?$1=$2 [L,QSA]
RewriteRule ^([^\+]+)\+([^/]+)/?$ /?$1=$2 [L,QSA]
Once you're sure it is working change R to R=301