简体   繁体   中英

URL rewriting with parameters in .htaccess

I've been searching lot of related tutorials and so on from Google to solve this on my own, but with zero luck. Therefore I am here to ask. I am trying to 'prettify' my project URL by rewriting. I am not sure are these all achievable anyhow, because I am just starting to get my head around the subject.

I am working 'example' on localhost project folder localhost/example. File '.htaccess' is located in that folder. Where I have set the following:

RewriteEngine On 
RewriteBase /example

So basically my application now generates a URL consisting at least 1 parameter all the time and another pointing current location.

Current URL: localhost/example/admin.php?e=2&p=frontpage

Fantasy: localhost/example/admin/2/frontpage

About the parameters:

  • p stands for selected page
  • e stands for event

Okay lets think this all is achievable easily, do I have to change all the attributes to match current shown url?

Now they are: href="?e=2&p=settings"

Should they be: href="2/settings" ?

I am checking what value GET parameter P has, then including that page into content area.

That is pretty much it, pretty too complex for me, but for education purposes I really want to understand this thru and thru. Thank you.

EDIT:

With the added 
RewriteRule ^admin.php/(.*)$ /admin.php?e=$1 [L,QSA]

I am getting lot of pathing errors, whole site is without styling and js files.

EDIT 2:

RewriteEngine On 
RewriteBase /example

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /admin.php/e=?(.*)$/p=?(.*)$ /admin.php?e=$1?p=$2 [L,QSA]

Now urls are following:
http://localhost/example/admin.php/2/inc/vex/vex.css
http://localhost/example/admin.php/2/css/modestgrid.css

It is not showing the page in url and the paths are not correct. 
They should be http://localhost/example/admin.php/css/modestgrid.css

Your question is a bit vague, contradictory and it is unclear how you actually want to handle (reference) your asset files. But in general I'd say this should be a starting point to get you going:

RewriteEngine On 
RewriteBase /example

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/(.*)$ $1.php?e=$2&p=$3 [END]

For this to work you obviously need the apache rewriting module to be installed and loaded, you need to take care that the interpretation of dynamic configuration files is enabled at all ( AllowOverride directive) and you have to place such file in the correct location with reading permission for the http server process.

In case you get an internal server error (http status 500) for that chances are that you operate a very old version of the apache http server. In that case you probably need to replace the [END] flag with the [L] flag which probably will work here too. You will find a hint on that in your http servers error log file in that case.


And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files ( .htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM