0

I want to modify the URL on my server using .htaccess but I'm facing some issues.

Checkout #Q2. here.

Rule #1:

https://example.com/site/index.php

https://example.com/site/

should get converted to short url.

https://example.com/

Rule #2:

https://example.com/

should load full url without expanding.

https://example.com/site/index.php.

Rule #3:

https://example.com/a/site/index.php

https://example.com/b/site/index.php

https://example.com/a/site/

https://example.com/b/site/

should get converted to short url.

https://example.com/a/

https://example.com/b/

Note that: I want to remove the site folder from URL's and I don't know to write .htaccess, any help would be much appreciated.

  • Please show your current .htaccess. Also are `/site/` or `/a/site/` real directories? – anubhava Feb 08 '21 at 19:15
  • My current `.htaccess` is empty, I just tried the solution from the question that I mentioned, and yes `localhost/site/` and `localhost/a/site/` are real directories and I will change the names in the future. –  Feb 08 '21 at 19:18
  • @Arghadip, For `Rule #4: should not block other URL's` could you please do provide some samples which urls you are talking about here? – RavinderSingh13 Feb 08 '21 at 19:21

1 Answers1

1

You may try these rules in your site root .htaccess (assuming there are no other .htaccess in sib directories):

RewriteEngine On

# matches /site/ or /site/index.php or 
# /a/site/index.php or /b/site/index.php
# captures a/ or b/ or an empty string in %1
# redirects to /a/ or /b/ or /
RewriteCond %{THE_REQUEST} \s/+([ab]/|)site/(?:index\.php)?[?\s] [NC]
RewriteRule ^ /%1 [L,R=302,NE]

# matches a/ or b/ or empty (landing page)
# rewrites to a/site/index.php or b/site/index.php or site/index.php
RewriteRule ^([ab]/)?$ $1site/index.php [L,NC]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643