Home > Mobile >  htaccess redirect all pages to subdirectory except root
htaccess redirect all pages to subdirectory except root

Time:01-11

enter image description hereI have these files in my root, Inside pages i have php files for example projects.php, I tried writing in .htaccess rules to take the file for example xxx.com/projects to open the file from /pages/projects.php and it worked with the following

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} . [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# No file extension on request then append .php and rewrite to subdir
RewriteCond %{REQUEST_URI} /(. )
RewriteRule !\.[a-z0-4]{2,4}$ /pages/%1.php [NC,L]

# All remaining requests simply get rewritten to the subdir
RewriteRule (.*) /pages/$1 [L]

My problem here is when i go to the root xxx.com instead of opening index.php its opening pages directory but if i go explicitely to xxx.com/index.php it works. I dont want index.php to be shown in the url i need to exclude the root from my rule and make it open index.php while the url stays xxx.com

CodePudding user response:

# All remaining requests simply get rewritten to the subdir
RewriteRule (.*) /pages/$1 [L]

To exclude "the root" being rewritten to /pages/ (and serve index.php from the root instead) you can simply change the quantifier in the last rule from * (0 or more) to (1 or more) - so that it doesn't match requests for the root (an empty URL-path in .htaccess).

In other words:

RewriteRule (. ) /pages/$1 [L]

Incidentally, you have already done something similar in the preceding rule/condition by using in the CondPattern, ie. RewriteCond %{REQUEST_URI} /(. ).

CodePudding user response:

I thought about this other solution:

RewriteEngine On

RewriteBase /

# Hide index.php from direct access via URL:
RewriteRule ^index\.php - [R=404]

# If it's not a file or a directory:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# If the page file exists:
RewriteCond %{DOCUMENT_ROOT}/pages/$0.php -f
# /your-page?param=foo is rewritten to /pages/your-page.php?param=foo
RewriteRule ^.*$ pages/$0.php [L,QSA]

You wanted to hide index.php. This can be done with a 404 error.

Then, for the rewrite itself, I would capture the URL with ^.*$ which will be available in the $0 backreference which can then be used in the rewrite rule itself and in the rewrite conditions.

If the request is not a directory or an existing file then we have to check that the resulting rewritten URL is effectively an existing PHP file in the pages directory.

I used the QSA flag so that you keep the query parameters in the resulting URL. The PHP script can then access them easily via $_GET. I expect you could also get them by checking some other environment variables if you don't use this flag.

You can still access directly /pages/your-page.php which you might want to avoid. This could be easily hidden by replacing the first rewrite rule to protect index.php by this more general rule:

# Hide all PHP files from direct access via URL:
RewriteRule \.php$ - [R=404]
  •  Tags:  
  • Related