简体   繁体   中英

Remove index.php with the GET superglobal from the URL

I have this code in my htaccess:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -l

RewriteRule ^(.+)$ index.php/?url=$1 [QSA,L]

And in PHP, I grab the current url request and give it it's controller so if there was http://example.com/index.php?url=about -> this will give me the about controller so it displays the about page (MVC)

Now my question is that how can I remove the index.php?url from every page. For example I want to access the about page then I need it to be -> http://example.com/about

You can see in the htaccess that I am replacing the index.php/?url with whatever I type ($1) But it doesn't seem to work because when I request this url: (example) http://example.com/about I get Object not found! Error 404. And when I have http://example.com I get the controller_home page. However, when i type http://example.com/home it also displays error 404.

If I do this: http://example.com/?url=about or http://example.com/index.php?url=about it works find and gives me the about controller so the ?url isn't getting removed by the htaccess I guess...

Might that be an another error in my php code or what?

Kindly help me as I have been looking over this error about 10 hours and I didn't find why it is behaving like that, my friend has almost the same code and it works perfectly for him.

I am kind of new to php so it might be simple bug... Thanks in advance for everyone that helps!

If you're using an .htaccess file

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^/?(.+)$ index.php?url=$1 [QSA,L]

If you're editing the main apache config file (httpd.conf or apache2.conf). I know the OP is using .htaccess but just in case other people having this issue see this discussion:

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-l

RewriteRule ^/?(.+)$ index.php?url=$1 [QSA,L]

First, presumably you want to test if the requested string does NOT match a directory, NOT match a file, and NOT match a symbolic link - so you need add a "!" before the RewriteCond flags (to negate the condition). Stacked RewriteConds are linked with an implicit AND so checking for directory, file and symbolic link doesn't make sense (a given requested string wouldn't match all of these conditions).

Second, the %{DOCUMENT_ROOT} would be necessary if you're doing this in the main apache config file (ie/httpd.conf or apache2.conf). It's not necessary if you're doing this in an .htaccess file.

Third, when RewriteRule is matching it includes the /. Presumably when you request "website.com/about" you want "about" to be passed to index.php not "/about", so the "/?" will remove the / in first character position from $1. I put the question mark, just because I think it's best practice, since technically you could get an HTTP request that does not begin with a slash (although this would be against standards and every browser includes a slash at the beginning).

Fourth, you shouldn't put a / after the index.php in RewriteRule (so it should be "index.php?url=$1", not "index.php/?url=$1". Query strings are separated from a filename via a "?" not a "/?".

Try this

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -l

RewriteRule ^(.*)$ http://%1/$1/ [R=301,L]
RewriteRule ^((.*)+)$ index.php?url=$1 [QSA,L]

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