A few questions...

First, is there anything I can consolidate on this .htaccess and keep all of the functionality?

Is my htaccess file going to be hidden from users with lines 2-5 or is there something else I need to do?

My last one is probably the most fun. As you can see I have a redirect to (both a WWW and .PHP redirect). However, my conflict is with the last line and that's partially because of what I just mentioned. I have a product pitch page which is on index.php and I also have a navigation button on my site that says "Order" which links you to index.php#Order (at the bottom of my page).

The conflict is that clicking on the index.php#Order link redirects you to index.php without the anchor. Anyone know how to make an exception to my rewrite rules so that my index.php anchor works properly?

Thank you so much!

DirectoryIndex index.php
<Files ~ "^\.ht">
    Order allow,deny
    Deny from all
</Files> 
ErrorDocument 404 /404.php
Options +FollowSymLinks
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.php\ HTTP/ 
RewriteRule ^(.*)index\.php$  [R=301,L]
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$  [R=301,L]
redirect 301 /order.php 

Dani AI

Generated

Two things are happening here that together explain the lost anchor.

First: the fragment (the part after #) is never sent to the server — it’s handled entirely by the browser. When you click a link to index.php#Order the browser requests /index.php (no #Order) and your rewrite that strips index.php then issues a 301 to /, so the browser never has a chance to jump to the anchor.

Second: as you remembered, # starts a comment in an Apache config file. That means a Redirect line like the one you showed will be truncated at the # and the fragment will not make it into the Location header even if you intended it to.

Practical fixes (pick one):

  • If the anchor is on the same page, make the nav link a fragment-only link: href="#Order". That performs a client-side jump and never hits the server.

  • If you need an external or permanent redirect from /order.php, do it in a small script (PHP/HTML) so you can emit a Location header that contains the fragment. Example PHP redirect:

    <?php
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: https://www.mysite.com/#Order");
    exit;
    ?>

    That avoids the .htaccess #-comment problem and prevents a second redirect to / because the Location goes straight to / with the fragment.

  • If you must keep a server-side redirect in .htaccess, point it to / (or use a script), not to index.php#Order (the # will be treated as a comment). Better: change the link to /#Order (or https://www.mysite.com/#Order) so the browser requests / directly and no index.php removal redirect runs.

Other notes: consolidate duplicate Options +FollowSymLinks and RewriteEngine On lines, test changes with temporary 302 redirects before switching to 301, and use browser DevTools (Network tab) to inspect the redirect chain and Location headers. For hiding dotfiles, on Apache 2.4 use Require all denied inside a FilesMatch; on older Apache you can keep Deny from all. This should resolve the anchor problem without breaking your canonical index.php → / rule.

I just remembered that # comments out the line in Apache .htaccess. Any other ideas, should I use some kind of DHTML/AJAX script to just slide or jump down? Any suggestions?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.