Solution 1 :

#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]

The second condition is never successful so the rule is not processed.

The second condition (that supposedly checks that the request + .php exists) is not what you should be doing here. If anything, you would need to check that the request does not map to a file. For example:

RewriteCond %{REQUEST_FILENAME} !-f

However, if you restrict the regex in your RewriteRule pattern by appending a $ (end-of-string anchor) then you can remove that condition altogether. For example:

#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+)$ cms/tests/index.php?survey=$1&mode=$2 [NC,L]

A request that matches the regex as stated could not map to a real file (unless you have extensionless files).


Aside:

# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

The second condition in your first code block is also not strictly correct and will fail under certain conditions, because the filesystem check is not necessarily the same as the file you will ultimately rewrite to, which could result in a 404 or 500 (rewrite loop) depending on your file structure. (This code block is surprisingly common, but issues relating to this keep on cropping up.)

This should really be written more like this:

# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

Now, the condition %{DOCUMENT_ROOT}/$1.php matches the rewrite substitution $1.php. (Assuming the .htaccess file is in the document root.)

See my answer to the following question on ServerFault with a detailed explanation of this change: https://serverfault.com/questions/989333/using-apache-rewrite-rules-in-htaccess-to-remove-html-causing-a-500-error

Problem :

Hello there i have a little problem.

When using these lines of .htaccess

RewriteEngine on

Options +FollowSymLinks

# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^cms/tests/([0-9]+) cms/tests/index.php?survey=$1 [NC,L]

I transform the url
cms/tests/index.php?survey=65 to -> cms/tests/65

This works!

Now i want to have an edit mode like this:

cms/tests/65/edit or cms/tests/65/view

This is the rewrite rule I have come up with:

RewriteEngine on

Options +FollowSymLinks

# clean up file extensions .php only
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

#make get request clean
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^cms/tests/([0-9]+)/([a-zA-Z0-9_-]+) cms/tests/index.php?survey=$1&mode=$2 [NC,L]

When writing the full url like this cms/tests/index.php?survey=65&mode=edit it completely works

But it with the “clean” url it goes to my 404 page and also shows a 302 redirect in the network tab.

Network tab

Is there a something that I am doing wrong or over seeing?

Comments

Comment posted by Vasco

Thank you very much for your comment much <3

Comment posted by image

I have one little problem again… When using your method for the get request I still get an error:

Comment posted by MrWhite

That is most probably caused by using relative URL-paths in your client side code (resulting in “404” errors – which your code is trying to interpret as JSON). You need to use root-relative (starting with a slash) or absolute URLs throughout. When you are at the URL

By