0

My current project needs some cleaner urls. I got it until the ID part like http://test.com/profile/1337(additional backslash)

I want to hide the "ugly" stuff like http://test.com/profile.php?plid=1337&action=view that would appear. Users shouldn't see this.

Already tried to add some "hardcoded" params like

RewriteRule ^profile/([0-9\_]+)/history/?$ ./profile.php?plid=$1&action=history [NC,QSA]

Also tried to change [NC,QSA] to [L,QSA], [QSA,L], [NC,QSA,L] and so on.

These are my rewrite rules currently not working

RewriteRule ^profile/([0-9\_]+)/edit/?$ ./profile.php?plid=$1&action=edit [NC,QSA]
RewriteRule ^profile/([0-9\_]+)/history/?$ ./profile.php?plid=$1&action=history [NC,QSA]

And this rule is working fine

RewriteRule ^profile/([0-9\_]+)/?$ ./profile.php?plid=$1&action=view [NC,QSA]

I want to display some buttons like "history, edit" if the action is "view" (which works fine at the moment)

Expecting a working url like https://test.com/profile/1337/history (Where $action should be 'history')

My error is currently a 404 page not found.

[Sat Sep 07 11:36:20.981057 2019] [:error] [pid 20923] [client ip:port] script '/var/www/main/hk/profile.php' not found or unable to stat

4
  • Your question is unclear. You stated (roughly) what goal you have in mind. But not what you actual question is. What is working with the rules you setup? And what is apparently not working as desired, since you try to ask here? Please be more specific in what detail exactly you fail to achieve. Commented Sep 7, 2019 at 10:10
  • Oh sorry. My Bad. I just update the question right now. Thanks for your note. Commented Sep 7, 2019 at 11:31
  • And what is not working? Though the rules you implemented certainly could be improved they should actually work. So what is the specific issue you want to ask about? Commented Sep 7, 2019 at 11:34
  • If I go to the profile/1337 page, the param $action is set to view If I enter profile/1337/ results in a 404 error. If I go to the url for example profile/1337/history results in 404, can't determinante the value $action anymore because the 404 error. Commented Sep 7, 2019 at 12:21

1 Answer 1

1

Here is a slightly modified version of your rule set. I remove the case insensivity (why should that matter?) and also the QSA flag since it is standard anyway. Using the END flag instead of L with save you a lot of hassle, if your http server supports it , more on that further down.

RewriteEngine on
RewriteRule ^/?profile/([0-9\_]+)/?$         /profile.php?plid=$1&action=view    [END]
RewriteRule ^/?profile/([0-9\_]+)/view/?$    /profile.php?plid=$1&action=view    [END]
RewriteRule ^/?profile/([0-9\_]+)/edit/?$    /profile.php?plid=$1&action=edit    [END]
RewriteRule ^/?profile/([0-9\_]+)/history/?$ /profile.php?plid=$1&action=history [END]

Make sure you are not looking at earlier results cached on the client side. So disable your browser cache for that site or use a fresh anonymous tab for testing.

In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.

This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. This explanation helped me a lot! Kudos to you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.