Avatar of sabecs
sabecs
 asked on

.htaccess - help needed with RewriteRule

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
Apache Web Server

Avatar of undefined
Last Comment
sabecs

8/22/2022 - Mon
Amar Bardoliwala

Hello sabecs,

you should use following

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

Hope this will help you.

Thank You.

Amar Bardoliwala
sabecs

ASKER
Thanks Amar for your help, I tried your suggestion but its not redirecting?
Amar Bardoliwala

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
sabecs

ASKER
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?
ASKER CERTIFIED SOLUTION
Amar Bardoliwala

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
sabecs

ASKER
Thanks Amar for your help, much appreciated. Sorry for late response..