简体   繁体   中英

URL rewriting doesn't work right after adding a query string line into it

This URL rewriting lines work fine till the query string line , and after the query string any of them not work.

ReWriteEngine On
ReWriteRule ^home?$ /mysite/index.php
ReWriteRule ^gallery?$ /mysite/gallery.php
ReWriteRule ^([a-z0-9]+)?$ /mysite/owner.php?name=$1
ReWriteRule ^about?$ /mysite/about.php
ReWriteRule ^location?$ /mysite/location.php

The first two lines home and gallery works as expected (including the third line query string ), but the other two lines about and location don't work. If I requested those two pages with the rewritten name , the pages not comes to browser (the response is nothing, besides I am getting the same page with i am currently on - not even getting a 404 error message), but if I requested those pages with the original name with the .php extension the page comes to the browser. I tried putting those two about and location at the top of the query string line, then those two pages work fine as expected , but when they are below of the query string line then only they don't work. Can I know what is wrong with the URL Rewriting methoed? I still can't able to figure the problem out.

There are a number of issues with your attempt, here is a slightly modified version:

ReWriteEngine On
ReWriteRule ^/?home/?$ /mysite/index.php [END]
ReWriteRule ^/?gallery/?$ /mysite/gallery.php [END]
ReWriteRule ^/?about/?$ /mysite/about.php [END]
ReWriteRule ^/?location/?$ /mysite/location.php [END]
ReWriteRule ^([a-z0-9]+)/?$ /mysite/owner.php?name=$1 [END]

The order of rules is important. The more general ones should be placed below the more specialized ones.

In case you experience a http status 500 (server internal error) with above setup chances are that you operate a very old version of the apache http server. In that case replace the [END] flag with the [L] flag which should also work fine in this scenario.


Above rules will work likewise in the real http servers host configuration and in dynamic configuration file.

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).

I would change the order of these

ReWriteRule ^([a-z0-9]+)?$ /mysite/owner.php?name=$1
ReWriteRule ^about?$ /mysite/about.php
ReWriteRule ^location?$ /mysite/location.php

To

ReWriteRule ^about$ /mysite/about.php [L]
ReWriteRule ^location$ /mysite/location.php [L]
ReWriteRule ^([a-z0-9]+)$ /mysite/owner.php?name=$1 [L]

And add the last flag.

what happens is that the ^([a-z0-9]+)? is more generic and matches and changes the URL before the last 2 rules are reached.

In other words, if you had a URL with about it matches this ^([a-z0-9]+)? gets changed and then does not match the actual rule for it. So by putting the more generic rule last you can avoid this.

Also as I said in the comments, I would add the [L] flag in or Last so that it ends the rewriting once a match is done, otherwise you can get unexpected matches.

You can also combine some of these (probably)

 ReWriteRule ^(about|location)$ /mysite/$1.php [L]

I am pretty sure that the ? means the preceding match is optional, but I removed it because if not then the capture group is optional. So I am not sure what the purpose you had of having it there. Because as I read it this means ^about?$ with the t being optional...

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