Home > other >  .htaccess Rewriterule redirecting to a URL on the same domain
.htaccess Rewriterule redirecting to a URL on the same domain

Time:02-01

I am trying to redirect users to specific URLs. I want to create .htaccess rewriterules to accomplish this.

I would lke for these:

https://example.com/career_by_education
https://example.com/international_careers
https://example.com/major_careers
https://example.com/career

to transfer to:

https://example.com/career_by_education/careers-by-educational-level.php
https://example.com/international_careers/international_careers.php
https://example.com/major_careers/academic_major_careers.php
https://example.com/career/career.php

I have tried many variations of rewrite rules to get this to work, and am successful if I want it to redirect to a different domain.

For example, the following (in .htaccss):

RewriteRule career_by_education   https://example2.com/career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers   https:/example2.com/international_careers/international_careers.php [L,R]
RewriteRule major_careers   https://example2.com/major_careers/academic_major_careers.php [L,R]
RewriteRule career   https://example2.com/career/career.php [L,R]

Successfully transfer to (respectively):

https://example2.com/career_by_education/careers-by-educational-level.php
https://example2.com/international_careers/international_careers.php
https://example2.com/major_careers/academic_major_careers.php
https://example2.com/career/career.php

But I want the redirect to be to the same domain (example.com), so I tried this (in this order):

RewriteRule career_by_education   https://example.com/career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers  https://example.com/international_careers/international_careers.php [L,R]
RewriteRule major_careers   https://example.com/major_careers/academic_major_careers.php [L,R]
RewriteRule career   https://example.com/career/career.php [L,R]

When I enter any of these into the browser:

https://example.com/career_by_education
https://example.com/international_careers
https://example.com/major_careers

I always get this URL:

https://example.com/career/career.php

I tried to reverse the ordering of the RewriteRules to:

RewriteRule career   https://example.com/career/career.php [L,R]
RewriteRule major_careers   https://example.com/major_careers/academic_major_careers.php [L,R]
RewriteRule international_careers  https://example.com/international_careers/international_careers.php [L,R]
RewriteRule career_by_education   https://example.com/career_by_education/careers-by-educational-level.php [L,R]

But the resulting URL is always:

https://example.com/career/career.php

I also tried to remove the full URL for the substitution, as follows:

RewriteRule career_by_education /career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers /international_careers/international_careers.php [L,R]
RewriteRule major_careers /major_careers/academic_major_careers.php [L,R]
RewriteRule career /career/career.php [L,R]

But the result always redirects to this URL:

https://example.com/career/career.php

I have tried different expressions to see if it can work:

RewriteRule ^/career http://consul64.wwwaz1-ts107.a2hosted.com/career/career.php [L,R]
RewriteRule ^/major_careers http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]
RewriteRule ^/international_careers http://consul64.wwwaz1-ts107.a2hosted.com/international_careers/international_careers.php [L,R]
RewriteRule ^/career_by_education http://consul64.wwwaz1-ts107.a2hosted.com/career_by_education/careers-by-educational-level.php [L,R]

These all result in a "403 Forbidden" error - From the command line I ensured that I have a "index.php" in the root directory, and permissions were correct, with the following:

chmod 644 ~/public_html/.htaccess
chmod 755 ~/public_html

Then I tried these:

RewriteRule .*(?=major_careers) http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]
RewriteRule .*(?=international_careers) http://consul64.wwwaz1-ts107.a2hosted.com/international_careers/international_careers.php [L,R]
RewriteRule .*(?=career_by_education) http://consul64.wwwaz1-ts107.a2hosted.com/career_by_education/careers-by-educational-level.php [L,R]
RewriteRule  .*(?=career) http://consul64.wwwaz1-ts107.a2hosted.com/career/career.php [L,R]

The resulting URL was always:

https://example.com/career/career.php

I don't understand why the pattern seems to work when redirecting to an outside URL, yet does not work if redirecting to a URL on the same domain.

Could anyone please help guide me?

CodePudding user response:

RewriteRule career_by_education /career_by_education/careers-by-educational-level.php [L,R]
RewriteRule international_careers /international_careers/international_careers.php [L,R]
RewriteRule major_careers /major_careers/academic_major_careers.php [L,R]
RewriteRule career /career/career.php [L,R]

The first argument to the RewriteRule directive (the pattern) is a regular expression. This is not an "exact string match". The problem with these rules is that the pattern also matches the redirected URL so would result in an endless "redirect loop", since the "regex" career_by_education matches that string anywhere in the URL-path.

You state that it always redirects to /career/career.php (the last rule above), but that's not possible with the rules as posted - due to the redirect-loop as mentioned above. However, it would happen if you reversed these directives (which you say you had also tried), since the first rule (that matches career only) would always match - but again, that is a redirect loop.

Aside: It might not trigger a redirect loop depending on your server config, eg. if requests for .php files are proxied to a CGI PHP engine.

You need to be more specific with the regex and match only the stated URLs. ie. You need to include anchors on the regex and match career_by_education only and not career_by_education anywhere.

For example:

RewriteRule ^career_by_education$ /career_by_education/careers-by-educational-level.php [L,R]
RewriteRule ^international_careers$ /international_careers/international_careers.php [L,R]
RewriteRule ^major_careers$ /major_careers/academic_major_careers.php [L,R]
RewriteRule ^career$ /career/career.php [L,R]

The order of these directives does not matter.

You don't necessarily need the scheme hostname (ie. an absolute URL) in the substitution if you are redirecting to the same domain, however, it can help to reduce the number of redirects in the case of domain canonicalisation.

I don't understand why the pattern seems to work when redirecting to an outside URL

Because the directives are not processed again after the redirect (you are now on a different server).

RewriteRule .*(?=major_careers) http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]

Using the unanchored positive lookahead is effectively the same as simply major_careers.

RewriteRule ^/career http://consul64.wwwaz1-ts107.a2hosted.com/career/career.php [L,R]
RewriteRule ^/major_careers http://consul64.wwwaz1-ts107.a2hosted.com/major_careers/academic_major_careers.php [L,R]
RewriteRule ^/international_careers http://consul64.wwwaz1-ts107.a2hosted.com/international_careers/international_careers.php [L,R]
RewriteRule ^/career_by_education http://consul64.wwwaz1-ts107.a2hosted.com/career_by_education/careers-by-educational-level.php [L,R]

These all result in a "403 Forbidden" error

Because of the slash prefix, these rules would never match, so they wouldn't actually do anything.


UPDATE:

However, a "problem" with these redirects is that the URL you are redirecting from, eg. /career_by_education is also the name of the filesystem directory you are redirecting to. So, by default, mod_dir will attempt to append a trailing slash, so the rule doesn't match. The 403 likely results from the absence of a DirectoryIndex document in that directory. Strictly speaking, your URLs need to end in a trailing slash, or make the trailing slash optional in the regex (but that could result in a double redirect).

For example:

# Trailing slash is mandatory
RewriteRule ^career_by_education/$ /career_by_education/careers-by-educational-level.php [L,R]

# Trailing slash is optional
RewriteRule ^career_by_education/?$ /career_by_education/careers-by-educational-level.php [L,R]

Although it would be preferable that the URL did not match the name of the directory in the first place.

  • Related