How to do a 301 redirect of dynamic pages with query strings to static pages?
I used to have a photo gallery that dynamically generated 1 page per photo, assigning each page's url a unique query string, such as:
https://www.example.com/photos/index.php?start=1
and
https://www.example.com/photos/index.php?start=2
Since I don't have a ton of photos in my gallery, I've turned each page into static urls (which might help their seo rankings). Those same pages now have static urls such as:
https://www.example.com/photos/winterstorm2007.php
and
https://www.example.com/photos/new-chickens.php
Here is what I tried in the htaccess file inside my photos directory -- which just throws a 404 error:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index.php?start=1 [NC]
RewriteRule (.*) /photos/winterstorm2007.php [R=301,L]
RewriteCond %{QUERY_STRING} ^index.php?start=2 [NC]
RewriteRule (.*) /photos/pastured-chickens.php [R=301,L]
apache .htaccess mod-rewrite url-rewriting
add a comment |
I used to have a photo gallery that dynamically generated 1 page per photo, assigning each page's url a unique query string, such as:
https://www.example.com/photos/index.php?start=1
and
https://www.example.com/photos/index.php?start=2
Since I don't have a ton of photos in my gallery, I've turned each page into static urls (which might help their seo rankings). Those same pages now have static urls such as:
https://www.example.com/photos/winterstorm2007.php
and
https://www.example.com/photos/new-chickens.php
Here is what I tried in the htaccess file inside my photos directory -- which just throws a 404 error:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index.php?start=1 [NC]
RewriteRule (.*) /photos/winterstorm2007.php [R=301,L]
RewriteCond %{QUERY_STRING} ^index.php?start=2 [NC]
RewriteRule (.*) /photos/pastured-chickens.php [R=301,L]
apache .htaccess mod-rewrite url-rewriting
add a comment |
I used to have a photo gallery that dynamically generated 1 page per photo, assigning each page's url a unique query string, such as:
https://www.example.com/photos/index.php?start=1
and
https://www.example.com/photos/index.php?start=2
Since I don't have a ton of photos in my gallery, I've turned each page into static urls (which might help their seo rankings). Those same pages now have static urls such as:
https://www.example.com/photos/winterstorm2007.php
and
https://www.example.com/photos/new-chickens.php
Here is what I tried in the htaccess file inside my photos directory -- which just throws a 404 error:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index.php?start=1 [NC]
RewriteRule (.*) /photos/winterstorm2007.php [R=301,L]
RewriteCond %{QUERY_STRING} ^index.php?start=2 [NC]
RewriteRule (.*) /photos/pastured-chickens.php [R=301,L]
apache .htaccess mod-rewrite url-rewriting
I used to have a photo gallery that dynamically generated 1 page per photo, assigning each page's url a unique query string, such as:
https://www.example.com/photos/index.php?start=1
and
https://www.example.com/photos/index.php?start=2
Since I don't have a ton of photos in my gallery, I've turned each page into static urls (which might help their seo rankings). Those same pages now have static urls such as:
https://www.example.com/photos/winterstorm2007.php
and
https://www.example.com/photos/new-chickens.php
Here is what I tried in the htaccess file inside my photos directory -- which just throws a 404 error:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^index.php?start=1 [NC]
RewriteRule (.*) /photos/winterstorm2007.php [R=301,L]
RewriteCond %{QUERY_STRING} ^index.php?start=2 [NC]
RewriteRule (.*) /photos/pastured-chickens.php [R=301,L]
apache .htaccess mod-rewrite url-rewriting
apache .htaccess mod-rewrite url-rewriting
edited Jan 3 at 17:59
halfer
14.7k759116
14.7k759116
asked Jan 3 at 17:06
joeinfojoeinfo
146
146
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
QUERY_STRING
variable only contains query part after ?
in URL.
You may use THE_REQUEST
to match REQUEST_URI and query:
RewriteCond %{THE_REQUEST} /index.php?start=1s [NC]
RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]
RewriteCond %{THE_REQUEST} /index.php?start=2s [NC]
RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]
Also note trailing ?
in target URI to strip off previous query strings.
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
ok in that case adds
after pattern as in my updated answer.
– anubhava
Jan 3 at 19:30
1
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026730%2fhow-to-do-a-301-redirect-of-dynamic-pages-with-query-strings-to-static-pages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
QUERY_STRING
variable only contains query part after ?
in URL.
You may use THE_REQUEST
to match REQUEST_URI and query:
RewriteCond %{THE_REQUEST} /index.php?start=1s [NC]
RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]
RewriteCond %{THE_REQUEST} /index.php?start=2s [NC]
RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]
Also note trailing ?
in target URI to strip off previous query strings.
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
ok in that case adds
after pattern as in my updated answer.
– anubhava
Jan 3 at 19:30
1
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
add a comment |
QUERY_STRING
variable only contains query part after ?
in URL.
You may use THE_REQUEST
to match REQUEST_URI and query:
RewriteCond %{THE_REQUEST} /index.php?start=1s [NC]
RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]
RewriteCond %{THE_REQUEST} /index.php?start=2s [NC]
RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]
Also note trailing ?
in target URI to strip off previous query strings.
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
ok in that case adds
after pattern as in my updated answer.
– anubhava
Jan 3 at 19:30
1
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
add a comment |
QUERY_STRING
variable only contains query part after ?
in URL.
You may use THE_REQUEST
to match REQUEST_URI and query:
RewriteCond %{THE_REQUEST} /index.php?start=1s [NC]
RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]
RewriteCond %{THE_REQUEST} /index.php?start=2s [NC]
RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]
Also note trailing ?
in target URI to strip off previous query strings.
QUERY_STRING
variable only contains query part after ?
in URL.
You may use THE_REQUEST
to match REQUEST_URI and query:
RewriteCond %{THE_REQUEST} /index.php?start=1s [NC]
RewriteRule ^ /photos/winterstorm2007.php? [R=301,L]
RewriteCond %{THE_REQUEST} /index.php?start=2s [NC]
RewriteRule ^ /photos/pastured-chickens.php? [R=301,L]
Also note trailing ?
in target URI to strip off previous query strings.
edited Jan 3 at 19:30
answered Jan 3 at 18:47
anubhavaanubhava
534k48331408
534k48331408
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
ok in that case adds
after pattern as in my updated answer.
– anubhava
Jan 3 at 19:30
1
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
add a comment |
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
ok in that case adds
after pattern as in my updated answer.
– anubhava
Jan 3 at 19:30
1
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
thank you for your reply. the good news is that i no longer get the 404 error. and it does work properly for start=1 and start=2. however, i quickly expanded your code to include 20 more redirects and whether i visit example.com/photos/index.php?start=1 OR example.com/photos/index.php?start=15 they both now redirect to the first static page: example.com/photos/winterstorm2007.php so it appears to be acting immediately upon the first digit in the start variable.
– joeinfo
Jan 3 at 19:09
ok in that case add
s
after pattern as in my updated answer.– anubhava
Jan 3 at 19:30
ok in that case add
s
after pattern as in my updated answer.– anubhava
Jan 3 at 19:30
1
1
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
100% ABSOLUTELY PERFECT! THANK YOU!
– joeinfo
Jan 3 at 19:42
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026730%2fhow-to-do-a-301-redirect-of-dynamic-pages-with-query-strings-to-static-pages%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown