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.

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

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集