Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

.htaccess - help needed with RewriteRule

Posted on 2012-03-19
6
Medium Priority
?
392 Views
Last Modified: 2012-06-21
Hi,
I have some rewrite rules which cause links such as <a href="Coaches">Coaches</a> to redirect to a specific page which works fine, but if I have a link to a file that contains the same name then it will also redirect.

http://www.mywebsite.com/uploads/web_pages/3CD%20R%20and%20R%20Coaches.pdf

How should "RewriteRule Coaches index.php?page_id=262" be written to work for the first link but not the other?

Thanks in advance for your feedback.


Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]
RewriteRule ([0-9]+)(.*)\.html$ index.php?page_id=$1


RewriteRule Home\ Page index.php?page_id=1
RewriteRule Club\ Details index.php?page_id=14
RewriteRule Coaches index.php?page_id=262
0
Comment
Question by:sabecs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 37737371
Hello sabecs,

you should use following

RewriteRule ^Coaches(.*)  index.php?page_id=262

Hope this will help you.

Thank You.

Amar Bardoliwala
0
 

Author Comment

by:sabecs
ID: 37737570
Thanks Amar for your help, I tried your suggestion but its not redirecting?
0
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 37737687
Hello sabecs,

you are trying to say that your page is redirecting to index.php?page_id=262 for following

http://www.mywebsite.com/uploads/web_pages/3CD%20R%20and%20R%20Coaches.pdf

right?

Please try following

RewriteRule ^Coaches$ index.php?page_id=262

Also I think you should have following in your .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R]

Hope this will help you.

Thank You.

Amar Bardoliwala
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 

Author Comment

by:sabecs
ID: 37738208
Thanks again for your help, this is what i have in my .htaccess file,

SetEnv PHPRC /home/mywebsit/public_html
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]
RewriteRule ([0-9]+)(.*)\.html$ index.php?page_id=$1

RewriteRule Home\ Page index.php?page_id=1
RewriteRule Club\ Details index.php?page_id=14
RewriteRule About\ Us index.php?page_id=254
RewriteRule History index.php?page_id=256
RewriteRule Roles\ &\ Responsibilities index.php?page_id=257
RewriteRule Committee\ Descriptions index.php?page_id=258
RewriteRule Comps\ and\ Programs index.php?page_id=248
RewriteRule Policies\ and\ Guidelines index.php?page_id=259
RewriteRule Awards index.php?page_id=260
RewriteRule Insurance index.php?page_id=261
RewriteRule ^Coaches$ index.php?page_id=262
RewriteRule Players index.php?page_id=263
RewriteRule Team\ Managers index.php?page_id=264
RewriteRule Parents index.php?page_id=265
RewriteRule Contacts index.php?page_id=266
RewriteRule Store index.php?page_id=274
RewriteRule Price\ List index.php?page_id=276
RewriteRule Information index.php?page_id=229
RewriteRule Subscription\ Form index.php?page_id=235

Yes, previously  http://www.mywebsite.com/uploads/web_pages/3CD%20R%20and%20R%20Coaches.pdf was redirecting to index.php?page_id=262 which I don't want, so that has been fixed but now <a href="Coaches">Coaches</a> won't redirect either?
0
 
LVL 11

Accepted Solution

by:
Amar Bardoliwala earned 2000 total points
ID: 37738375
Hello sabecs,

Please backup of your old .htaccess file and see if following works as per your expectations

SetEnv PHPRC /home/mywebsit/public_html
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mywebsite.com$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule \.(css|jpe?g|gif|png|pdf|js|jpg|ico)$ - [L]

RewriteRule Home\ Page index.php?page_id=1
RewriteRule Club\ Details index.php?page_id=14
RewriteRule About\ Us index.php?page_id=254
RewriteRule History index.php?page_id=256
RewriteRule Roles\ &\ Responsibilities index.php?page_id=257
RewriteRule Committee\ Descriptions index.php?page_id=258
RewriteRule Comps\ and\ Programs index.php?page_id=248
RewriteRule Policies\ and\ Guidelines index.php?page_id=259
RewriteRule Awards index.php?page_id=260
RewriteRule Insurance index.php?page_id=261
RewriteRule Coaches index.php?page_id=262
RewriteRule Players index.php?page_id=263
RewriteRule Team\ Managers index.php?page_id=264
RewriteRule Parents index.php?page_id=265
RewriteRule Contacts index.php?page_id=266
RewriteRule Store index.php?page_id=274
RewriteRule Price\ List index.php?page_id=276
RewriteRule Information index.php?page_id=229
RewriteRule Subscription\ Form index.php?page_id=235

RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [L,R=301]
RewriteRule ([0-9]+)(.*)\.html$ index.php?page_id=$1


Hope this will help you.

Thank You.

Amar Bardoliwala
0
 

Author Closing Comment

by:sabecs
ID: 37769516
Thanks Amar for your help, much appreciated. Sorry for late response..
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Over the last year I have answered a couple of basic URL rewriting questions several times so I thought I might as well have a stab at: explaining the basics, providing a few useful links and consolidating some of the most common queries into a sing…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question