简体   繁体   中英

Apache rewrite issue with php parameters

I would like to access my page in following format: mysite.com/parameter instead of mysite.com/index.php?param=parameter. I played around with httpd.conf file and the best I managed was mysite.com/dir/parameter.

My conf is following:

        Options Indexes MultiViews Includes ExecCGI
        Options FollowSymLinks
        AllowOverride All
        Order allow,deny
        Allow from all
        RewriteEngine On
        RewriteRule /(.*)$ index.php?hash=$1 [NC] 

How can I get rid of the excessive directory?

Changing to:

           RewriteEngine On
           RewriteRule ^(.*) index.php?hash=$1 [NC]

Results in following error while trying to open the page

Forbidden

You don't have permission to access /Šw^Ƙi on this server.

If you have that configuration inside a .htaccess style file, then drop the leading slash ( / ) and prevent rewriting of requests to actually existing stuff:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?hash=$1 [L,NC]

You might also want to add an addition QSA flag in case you require additional parameters to be handed over.

If you place such configuration in the real server configuration (which is far the better choice if you have access), then your current setting should be just fine. .htaccess style files slow down the server considerably, they are notoriously error prone and hard to debug.

First of all, you have to make sure that you do not create an infinite loop by rewriting the url to something that matches again on the rewrite rule, this may be the cause of your directory isse:

RewriteRule /(.*) index.php?hash=$1 [NC,L]

Second, it is probably helpful to check if files are present, eg if you want to serve images or a robots.txt, this may also fix your loop problem, if you check for index.php being present:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.*) index.php?hash=$1 [NC,L]

requests for existing files will just fall through to the normal handling of local files.

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