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 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动