Hi Everyone,
I have copied a .htaccess file from one website and I am trying to use the same htaccess file on a different website.

here is the copy of the htaccess file I have copied from one site to another...

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^(\d+)/([^/]+)/?$ index.php?uiid=$1&title=$2 [L]
</IfModule>

both websites have the same structure as
/domname.com/dir1/dir2/shopping-cart/index.php
The htaccess file successfully rewrites the above url into the below url
/domname.com/dir0/dir1/shopping-cart/302/this-is-a-second-test-item

But for some reason, the htaccess file doesnt work & I get the error
The requested URL /domname.com/dir1/dir2/shopping-cart/index.php was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Can someone point me in the right direction with a fix to why this would not be working,
But, it works perfectly on the website I copied it from...

Dani AI

Generated

Likely cause: path context. On the site where it works, the .htaccess is probably sitting inside the same folder as shopping-cart/index.php, so the relative target index.php resolves correctly. On the other site, Apache is resolving that relative path against a different directory (or an addon-domain style path), so it tries to serve /domname.com/dir1/dir2/shopping-cart/index.php as a URL and 404s. The extra line about ErrorDocument suggests your 404 handler path is also wrong.

Try this, placing the file in the shopping-cart directory (or adjust RewriteBase to match where it lives):

Options -MultiViews

<IfModule mod_rewrite.c>
  RewriteEngine On

  # set to the URL path of the directory that contains this .htaccess
  RewriteBase /dir1/dir2/shopping-cart/

  # let real files/dirs pass through
  RewriteCond %{REQUEST_FILENAME} -f [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^ - [L]

  # pretty URL -> front controller
  RewriteRule ^([0-9]+)/([^/]+)/?$ index.php?uiid=$1&title=$2 [QSA,L]
</IfModule>

Notes and quick checks:

  • If you keep .htaccess at the document root instead, rewrite to an absolute target with a leading slash to remove ambiguity: /dir1/dir2/shopping-cart/index.php?....
  • Hit /dir1/dir2/shopping-cart/index.php directly. If that 404s, your file is not where you think or the vhost docroot differs.
  • The ErrorDocument reference means your custom handler points to a bad path; use a site-rooted path that exists (e.g., /404.php).
  • @LastMitch, host conditions and .html patterns are not needed for this case.
  • , whitespace at EOL is fine; the common gotcha is saving .htaccess with a BOM or odd encoding. Save as UTF-8 (no BOM), Unix line endings. Also confirm AllowOverride permits rewrites on the new host.

Recommended Answers

All 3 Replies

Member Avatar for Member #949455

Can someone point me in the right direction with a fix to why this would not be working,

Is dir0 and dir1 are URL keys? Then you need to create a new Mod for them. If those are not URL keys but a folder then you need to relink from the old one to the new one.

To rewrite as in URL Keys it should look something like this (it's not tested):

//Rewrite 1
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^([^/]+)/([^/]+).html /index.php?uiid=$1&title=$2 [NC,R]

//Rewrite 2
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^(.*)/(.*)\.html /index.php?uiid=$1&title=$2 [R=301,L]

Relink should be something like this (it's not tested either):

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^dir0\.html$ "http\:\/\/www\.domname\.com\/dir0\.php" [R=301,L]

RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^dir1\.html$ "http\:\/\/www\.domname\.com\/dir1\.php" [R=301,L]

Hi, the first thing to check out when YOU COPY IN CODES in htaccess file and the site breaks error 500 - is to make sure there are no whitespaces in the line end. Heads up on that.

What editor do you use? I never had an issue with whitespace when I was using Apache.

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.