Search code examples
apache.htaccessmod-rewrite

Why my htaccess is not working when i upload it to ionos webspace?


I've tried in in my localhost at it worked fine but after I upload it to my ionos webspace the website index is working but after I click the content it is not directing to anywhere and there is an error message:

Error 404 not foound, Your browser can't find the document corresponding to the URL you typed in.

Here is my .htaccess:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME}\.php -f

RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteRule ^news/([0-9a-zA-Z_-]+) news.php?url=$1 [NC,L]

RewriteRule ^seksikateg/([0-9a-zA-Z_-]+) seksikateg.php?kategori=$1 [NC,L]

and i placed he file in the same place as the index.php, news.php, and seksikateg.php


Solution

  • It's possible that MultiViews is enabled at your host and this will break your rules since this will append the .php extension before mod_rewrite processes the request.

    However, your directives are also in the wrong order. The generic rewrite to append the .php extension should appear after the other rules.

    Your rewrite to append the .php extension is not strictly correct as it could result in a rewrite loop (500 error) under certain circumstances.

    Try it like this instead:

    # Ensure that MutliViews is disabled
    Options -MultiViews
    
    RewriteEngine on
    
    RewriteRule ^news/([0-9a-zA-Z_-]+)$ news.php?url=$1 [NC,L]
    
    RewriteRule ^seksikateg/([0-9a-zA-Z_-]+)$ seksikateg.php?kategori=$1 [NC,L]
    
    # Append ".php" extension on other URLs
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^([^.]+)$ $1.php [L]
    

    I've also added the end-of-string anchor to the regex of your existing rewrites, otherwise you are potentially matching too much. eg. /news/foo/bar/baz would have also been rewritten to news.php?url=foo - potentially creating duplicate content and opening up your site to abuse.

    I would also question the use of the NC flag on these rewrites. If this is required then you potentially have a duplicate content issue.

    No need to backslash-escape literal dots in a regex character class and the NC flag is certainly redundant on the last rule.