douchen7366 2019-07-19 19:27
浏览 46
已采纳

如何将HTACCESS更改为指向项目所在的子文件夹

I have a project in a subfolder of my xampp root (rootFolder/myProjectName). Ok. But I'm using friendly URLs. So, whenever I want to link a JS or CSS file, for example, if I set:

<script type="text/javascript" src="/script.js"></script>

This is pointing to rootFolder/script.js not rootFolder/myProjectName/script.js!

Ok! I could add manually.. But, later, if I change my project to another folder or even to the root, I'd to change one by one!

How can I set the base folder of my project as "myProjectName" using htaccess (Or, if there is a better way..).

My htaccess is currently like this:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

This is because I'm using the "index.php" to route! But there are some pages called outside (http://localhost/myProjectName/html/pageName) and I cant get scripts to find their way without setting "[src="**myProjectName/**script.js"] manually!

  • 写回答

1条回答 默认 最新

  • dsk49208 2019-07-19 19:30
    关注

    You can done it with the next way with VirtualDocumentRoot

    <VirtualHost *:80>
        UseCanonicalName Off
        VirtualDocumentRoot /path_to_rootFolder/%0
    
        <Directory /path_to_rootFolder>
                Options +Indexes +FollowSymLinks +MultiViews
                AllowOverride All
                Require all granted
        </Directory>
    
        ErrorLog /path_to_logs/error_log
    
        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        # LogLevel debug
    
        LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
        CustomLog "/path_to_logs/access_log" vcommon
    

    Then add domain name to your hosts file e.g. "test.loc". Create test folder in your rootFolder and Apache will looks to test folder. Also when you will need new project you can create new folder with another name and add new domain for it into your hosts file. But you domain name and folder name should be the same.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?