duanbai1027 2017-03-30 09:22
浏览 74
已采纳

重写/隐藏.htaccess中的某些URL

I'm trying to get right syntax for .htaccess without any result...

I've a URL structured as domain.com/app/public/pageName .

It's working fine but I would "hide" the 'app/public/' part in browsers, basically doing something like:

[real URL] domain.com/app/public/pageName -> domain.com/pageName [what users type and see in browsers]

I think in that way it should be more readable and seo-friendly.

As I understood from docs (and maybe it's wrong because it's not working...) I should tell to Apache to map/redirect all URL like domain.com/pageName to domain.com/app/public/pageName , but only internally, in order to show the minimal URL in users' browsers.

Right now I have something like:

RewriteEngine on

#RewriteBase /app/public/

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+)$ https://localhost/app/public/index.php?url=$1 [QSA,L]

(I'm using full URL with https://... in order to get something that will be quick and easy to adapt when I upload all to my hosting, is it right?).

Problem is that RewriteRule actually change the URL, because it perform a redirect and URL rewrite it's not handle internally.

So, first of all: is it possible what I'm trying to do? If so, how can I handle the URL rewrite only internally?

Everything should be uploaded to a shared hosting, so I don't have other than .htaccess. Anyway, I can consider to upgrade to a vps if there are not other possibilities...

Thanks!

==============

EDIT (should be more clear now)

tl;dr version:

I'm looking for a method that let users to type domain.com/pageName (and they will see that address in their browsers) and rewrite internally that URL in order to point to domain.com/app/public/pageName.

==============

More: after /app/public/ there can be an arbitrary number of elements, separated by / . All of these elements are appended at the end of the URL after index.php. At the end URL looks like:

  domain/app/public/index.php?url=lot/of/elements/here 

This is already working with the RewriteRule posted above, I would keep that too. Thanks!

  • 写回答

2条回答 默认 最新

  • dshun123456 2017-03-31 08:58
    关注

    Just for reference, I found a solution, maybe will be usefull for someone.

    Basically I moved .htaccess to the root server (instead of /app/public directory) and changed the RewriteRule as follow:

    RewriteEngine on
    RewriteBase /app/public/
    
    #RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    
    RewriteRule ^(.*)$ index.php?url=$1 [PT]
    

    Now it's working (at least on localhost).

    What do you think? Are there any side effects with this config?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测