2
\$\begingroup\$

I wonder if the following 301 redirect is the best way for my situation. I used this post on SO to give me the following 301 redirect:

RewriteRule locations\.php$ compare-biomass.php [R=301]

What we've done is changed a filename from locations.php to compare-biomass.php.

The new URL should be:

www.mydomain.com/compare-biomass.php?variable1=1&variable2=2&variable3=3.

The above SO post mentioned that the old query string gets appended to the new filename, which is exactly what I want. However, I found that I was being redirected so that SERVER["DOCUMENT_ROOT"] was being appended to the domain, so the URL was:

www.mydomain.com/home/mydomain/public_html/compare-biomass.php?variable1=1&variable2=2&variable3=3

I worked out that I had to include the following in my .htaccess file

RewriteBase /

However, now that I've had a problem and this is the very first time I'm doing anything related to Apache, can you please tell me if the .htaccess file I'm using is optimal for me:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/1ドル [R=301,L]
RewriteBase /
RewriteRule locations\.php$ compare-biomass.php [R=301]
asked Mar 11, 2015 at 22:23
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Since the RewriteBase directive applies to the entire .htaccess file, it should not be buried between two RewriteRules.

You don't need capturing parentheses for the RewriteCond.

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule .* http://www.example.com/0ドル [R=301,L]
RewriteRule ^locations\.php$ compare-biomass.php [R=301]
answered Apr 11, 2015 at 21:37
\$\endgroup\$
-1
\$\begingroup\$
@author Darik
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/1ドル [R=301,L]
RewriteBase /
RewriteRule ^locations.php$ compare-biomass.php?1ドル [R=301,L]

If your server supports it, you don't have to put Options +FollowSymLinks in your .htaccess
I think the problem is that you forgot a backslash between locations and .php

\$\endgroup\$
3
  • \$\begingroup\$ hi unfortunately it doesn't, it simply redirects the page back to www,mydomain.com. \$\endgroup\$ Commented Mar 14, 2015 at 16:28
  • \$\begingroup\$ It won't even if there is an error it should redirect you to 404 page. The third and the forth lines are the one which redirects you to the home page. Try with out them and see what happens. Plus the web address should match the name you specify in the RewriteRule eg: RewriteRule ^biomass.php$ compare-biomass.php?1ドル [R=301,L] \$\endgroup\$ Commented Mar 15, 2015 at 17:23
  • \$\begingroup\$ compare-biomass.php?1ドル - this will result in the removal of the query string entirely (the 1ドル backreference is always empty), which is not what is required. The trailing ?1ドル should be removed (as it's stated in the question) - since the query string is passed through by default. \$\endgroup\$ Commented Sep 4, 2021 at 0:41

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.