url rewrite not working .htaccess

痴心易碎 提交于 2020-01-05 02:50:08

问题


I am trying to rewrite the url using htaccess and I have tried answers given on another questions here however nothing seems to be working at all I still get the original url. this is what I have:

http://localhost/inbox.php?pg=2

I want

http://localhost/inbox/2 

I already had a rule that gets rid of the .php extension in .htaccess as below and just added the last line

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]
RewriteRule /(.*)$ /inbox.php?pg=$1//added this

回答1:


Your problem is that the line before the last one is defined as the last one so your rule must be above that RewriteConditions. Better use this rule set:

RewriteRule ^/?inbox/(\d+)$ /inbox.php?pg=$1 [QSD,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [QSA,L]

I added your needed prefix which you missed and made it mandetory that after that numbers will follow (at least one).




回答2:


Apache rewrite engine is mainly used to turn dynamic url’s such as http://localhost/inbox.php?pg=2 into static and user friendly url’s http://localhost/inbox/2

RewriteEngine on
RewriteRule ^inbox/([^/.]+)/?$ /inbox.php?pg=$1 [L]

Explanation: How this work

Call to action: RewriteRule

Pattern: ^inbox/([^/.]+)/?$

Rewrite: /inbox.php?pg=$1

Command Flag: [L]




回答3:


Simply,

RewriteEngine on
RewriteRule ^inbox/([0-9]+)/?$ inbox.php?pg=$1

Source: 10 Simple examples to rewrite using htaccess



来源:https://stackoverflow.com/questions/23074903/url-rewrite-not-working-htaccess

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!