简体   繁体   English

使用mod_rewrite使URL更漂亮

[英]Using mod_rewrite to make a url prettier

I am trying to use mod_rewrite to change the appearance of my URL's. 我正在尝试使用mod_rewrite更改URL的外观。 I am really lost in this and could use some help. 我真的迷失了,可以使用一些帮助。 First of all, is it possible to use mod_rewrite to alter the "front-end" of a URL and keep it the same internally? 首先,是否可以使用mod_rewrite更改URL的“前端”并使之在内部保持相同? If so, how would I do the following: 如果是这样,我将如何执行以下操作:

I have https://domain.com/live/page.php?id=x with both "page" and "x" being variable, and would like the URL's to be shown as https://domain.com/page if possible, or if I need the id=x in there then it could be https://domain.com/page/x/ but I don't know if I need to have that id somewhere in the URL. 我有https://domain.com/live/page.php?id=x ,其中“ page”和“ x”都是变量,并且如果该网址显示为https://domain.com/page ,可能,或者如果我在那里需要id = x,那么它可能是https://domain.com/page/x/但我不知道我是否需要在URL的某处添加该ID。

Please help me as I know this is a common thing but I just can't figure it out! 请帮助我,因为我知道这是很平常的事情,但我只是想不通!

Thanks! 谢谢!

EDIT: With the help of anubhava I got this working halfway. 编辑:在anubhava的帮助下,我完成了这项工作。 This time I put the code into my .htaccess for the /live/ directory not the root one. 这次,我将代码放入/ live /目录而不是根目录的.htaccess文件中。 Now the pages change URL correctly, but I still need the server to load from the original so the new URL's are giving me a 404. Here is the code: 现在页面可以正确更改URL,但是我仍然需要服务器从原始页面加载,因此新的URL会给我一个404。这是代码:

RewriteEngine on

RewriteOptions inherit

RewriteCond %{THE_REQUEST} \s/+live/([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /live/$1.php?$2=$3 [L,QSA,NC]

EDIT: Here are my current .htaccess files 编辑:这是我当前的.htaccess文件

root: 根:

RewriteEngine on

RewriteOptions inherit

RewriteCond %{HTTP_HOST} ^bidwaffle\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.bidwaffle\.com$

RewriteRule ^/?$ "https\:\/\/bidwaffle\.com\/live" [R=301,L]

and in root\\live\\: 并在root \\ live \\中:

# secure htaccess file
<Files .htaccess>
 order allow,deny
 deny from all
</Files>

# disable directory browsing
Options All -Indexes

# disable access to logs/template files
<Files ~ "\.(log|tpl)$">
 order allow,deny
 deny from all
</files>

RewriteCond %{THE_REQUEST} \s/+live/([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/?$ /live/$1.php?$2=$3 [L,QSA,NC]

The URL changes properly but doesn't load properly (silent loading of the real URL). URL正确更改,但没有正确加载(真实URL的静默加载)。 Right now I see the issue of the second part where it may not even see this request because the /live/ is no longer in the "pretty URL" so the /live/.htaccess might not be called. 现在,我看到第二部分的问题,它可能甚至看不到此请求,因为/ live /不再位于“漂亮的URL”中,因此可能不会调用/live/.htaccess。 I tried putting that part into the root/.htaccess and it did the same weird thing with appending the referring page instead of %3 我尝试将其放入root / .htaccess中,并且通过添加引用页面而不是%3进行了同样的操作

Yes sure you can have rules like this in your DOCUMENT_ROOT/live/.htaccess file: 是的,请确保您的DOCUMENT_ROOT/live/.htaccess文件中可以包含以下规则:

RewriteEngine On
RewriteBase /live/

RewriteCond %{THE_REQUEST} /([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=302,L]

root .htaccess: 根.htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?bidwaffle\.com$
RewriteRule ^/?$ https://bidwaffle.com/live/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /live/$1.php?$2=$3 [L,QSA,NC]

I think you've got it backwards. 我想你已经倒退了。 The incoming URI from visitors (and links on your pages) is the SEO (pretty) form: /page/x . 来自访问者(以及页面上的链接)的传入 URI是SEO(漂亮)格式: /page/x It is the job of .htaccess to convert the pretty form into the real "dynamic" form: /live/page.php?id=x (where both page and x could be extracted from the SEO URI). .htaccess的工作是将漂亮的表单转换为真正的“动态”表单: /live/page.php?id=x x(其中,页面和x都可以从SEO URI中提取)。 .htaccess does not modify outgoing URIs. .htaccess不会修改传出 URI。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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