【问题标题】:Switching website's folder from www to non-www using htaccess使用 htaccess 将网站的文件夹从 www 切换到非 www
【发布时间】:2014-04-07 04:00:17
【问题描述】:

嗨,虽然这个问题被问了很多次,但我的问题有点不同,我尝试了很多东西但无法完成。

我有一个网站在 wordpress 中运行,域为“http://example.com”,另一个网站位于“/jobs”文件夹(一个简单的核心 php 网站)。现在我的问题是,当用户登陆“http://www.example.com/jobs”及其内部链接时,使用我们的 jquery 的重定向不起作用,因为对于重定向,我们使用 URL 作为没有“www”的基本 url。

现在我怎么可能使用 htaccess 将我的网站从 www 切换到非 www 仅用于文件夹及其所有内部文件夹。

我们根网站的Htaccess:

# Use PHP5.4 as default
AddHandler application/x-httpd-php54 .php
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

</IfModule>

# END WordPress


<ifModule mod_headers.c>
    Header unset Pragma
    FileETag None
    Header unset ETag
    <FilesMatch ".(js|css)$">
        Header set Cache-Control "max-age=29030400, private"
    </FilesMatch>
    <FilesMatch "\.(jpg|jpeg|png|gif|swf|ico)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
</ifModule>
<ifModule mod_deflate.c>
    <filesmatch "\.(js|css|html|swf|xml)$">
        SetOutputFilter DEFLATE
        # Netscape 4.x has some problems...
        BrowserMatch ^Mozilla/4 gzip-only-text/html

        # Netscape 4.06-4.08 have some more problems
        BrowserMatch ^Mozilla/4\.0[678] no-gzip

        # MSIE masquerades as Netscape, but it is fine
        # BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

        # NOTE: Due to a bug in mod_setenvif up to Apache 2.0.48
        # the above regex won't work. You can use the following
        # workaround to get the desired effect:
        BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

        # Don't compress images
        SetEnvIfNoCase Request_URI \
        \.(?:gif|jpe?g|png)$ no-gzip dont-vary

        # Make sure proxies don't deliver the wrong content
        Header append Vary User-Agent env=!dont-vary
    </filesmatch>
</ifModule>

我们的 /jobs 文件夹的 Htaccess:

suPHP_ConfigPath /home1/project_name/public_html/jobs
<Files php.ini>order allow,deny
deny from all
</Files>

RewriteEngine On
Options -Indexes

RewriteRule ^search/(.*) /jobs/?location[]=$1&q=$2&bookMark=$3&scrollFlag=$4&countScroll=$5&sort=$6 [L]

RewriteRule ^companies/(.*)/(.*)/(.*)$  /jobs/joblisting.php?comp_id=$1&jobRole=$2&pgno=$3 [L]
RewriteRule ^companies/(.*)/(.*)$  /jobs/joblisting.php?comp_id=$1&pgno=$2 [L]
RewriteRule ^companies/(.*)$  /jobs/joblisting.php?comp_id=$1 [L]

<ifModule mod_headers.c>
    Header unset Pragma
    FileETag None
    Header unset ETag
    <FilesMatch ".(js|css)$">
    #set max-age for 1 YEAR
        #Header set Cache-Control "max-age=29030400, private"
    #set max-age for NEVER CACHE
    Header set Cache-Control "max-age=0, private, no-store, no-cache, must-revalidate"
    </FilesMatch>
    <FilesMatch "\.(jpg|jpeg|png|gif|swf|ico)$">
        Header set Cache-Control "max-age=604800, public"
    </FilesMatch>
</ifModule>

# log PHP errors to a file
# File modified on Tue Feb 25 22:07:48 2014 by server
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives
# php_flag log_errors on
# File modified on Tue Feb 25 22:07:52 2014 by server
# For security reasons, mod_php is not used on this server. Use a php.ini file for php directives
# php_value error_log "/php_errors.log"



<ifModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file .(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

【问题讨论】:

    标签: php .htaccess redirect


    【解决方案1】:

    只需匹配主 .htaccess 中的作业文件夹,不要使用 www 。

    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/jobs
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
    

    或者如果你想全部在一行中。

    RewriteRule ^jobs/(.*)$ http://example.com/jobs/$1 [L,R=301]
    

    【讨论】:

    • 感谢您的回答,但不幸的是它在我的情况下不起作用。
    • 我有 2 个 htaccess 文件,一个在作业文件夹中,另一个在主域中。我尝试将代码放在两个 .htaccess 文件中。但没有一个
    • 您的第一个代码在某些时候可以工作,但是当网站在 example.com/jobs/$1 上重定向时,它会继续重定向,并且浏览器会显示相同的错误消息。
    • 好的,然后编辑您的问题并发布您的 htaccess 文件的内容,否则真的没有办法提供帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 2016-01-07
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多