F__M
F__M

Reputation: 1598

404 errors after url rewrite with .htaccess

I'm using url rewriting in my website.

I've these url:

works
example.com/_new/app/planning
example.com/_new/app/user_info

doesn't work (404)
example.com/_new/try
example.com/_new/features


Here my rules:

RewriteEngine On

# Removing extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

# example.com/app/[module]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?param1=$1 [L]

# example.com/app/[module]/[action]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2 [L]

# example.com/app/[module]/[action]/[type]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3 [L]

# example.com/app/[module]/[action]/[type]/[id]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ app.php?param1=$1&param2=$2&param3=$3&param4=$4 [L]

Here the file structure:

/_new/
/_new/try.php
/_new/login.php
/_new/app.php

The try.php and index.php should be shown as example.com/try or example.com/login.

The app.php loads module from another directory. This one should be: example.com/app/module_name.


What's the problem ?

Thanks.

Upvotes: 0

Views: 38

Answers (1)

KAD
KAD

Reputation: 11122

In htaccess, the base URL example.com will be skipped in the matching, and in your file, the first regex ([^\.]+) will match /_new/try and it tries to redirect you to a file /_new/try.php which does not exist, hence 404.

Make sure to test your regex before trying to use the URL, you can benefit from https://regex101.com/ to make your tests.

Upvotes: 2

Related Questions