1

I am trying to redirect a page with an ID querty string in it to a user friendly URL. I have multiple pages like this that I need to redirect within the htaccess file. Here is an example of one of the pages I am trying to redirect:

Old URL: examplepage.com/about_news_info.aspx?id=275

redirect to

New URL: examplepage.com/news-events/

This is what I am using with no luck:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=275$
RewriteRule ^/about_news_info.aspx http://examplepage.com/news-events/test1 [L,R=301]

RewriteCond %{QUERY_STRING} ^cateid=373$
RewriteRule ^/about_news.aspx http://examplepage.com/news-events/test2 [L,R=301]
0

2 Answers 2

1

.htaccess is per directory context vs putting it in the config file so your pattern is not matching.

From the documentation:

When using the rewrite engine in .htaccess files the per-directory prefix (which always is the same for a specific directory) is automatically removed for the RewriteRule pattern matching and automatically added after any relative (not starting with a slash or protocol name) substitution encounters the end of a rule set. See the RewriteBase directive for more information regarding what prefix will be added back to relative substitutions.

The removed prefix always ends with a slash, meaning the matching occurs against a string which never has a leading slash. Therefore, a Pattern with ^/ never matches in per-directory context.

So your rules should look like this without the leading / in the RewriteRule.

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=275$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about_news_info.aspx http://examplepage.com/news-events/test1 [L,R=301]

RewriteCond %{QUERY_STRING} ^cateid=373$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about_news.aspx http://examplepage.com/news-events/test2 [L,R=301]

Without checking for query string which really isn't needed.

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about_news_info.aspx$ http://examplepage.com/news-events/test1? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^about_news.aspx$ http://examplepage.com/news-events/test2? [L,R=301]
Sign up to request clarification or add additional context in comments.

7 Comments

I tried the code you provided but the URL is still not redirecting and going to a 404 page.
I'm curious where is the .htaccess code at that you have aspx pages? That is typically ran on an IIS server not apache.
This site was on a windows server, but is now on an apache server and I want all the old URLs to redirect to the new pages so that we do not get an increase in not found pages from Google Webmaster Tools.
You don't need to worry about the query string. Just make sure the file matches and it will redirect regardless of the query string. See my updated code.
There are multiple query strings per URL. For example there is examplepage.com/about_news_info.aspx?id=275 and examplepage.com/about_news_info.aspx?id=325 that each go to a separate page. So I will need to specify the query string. I appreciate your help.
|
0
RewriteEngine On

RewriteCond %{QUERY_STRING} ^id=275$
RewriteRule ^about_news_info.aspx$ http://examplepage.com/news-events/test1? [L,R=301]

RewriteCond %{QUERY_STRING} ^cateid=373$
RewriteRule ^about_news.aspx$ http://examplepage.com/news-events/test2? [L,R=301]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.