0

I don't know how to make this, I tried doing this and this but somehow it doesn't work on me. I don't know where it went wrong. My file system has only one folder for now and it just loops and on the url, it just add and add the folder's name. here is my code: .htaccess

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#php
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php
RewriteRule ^.+\.php$ /%1 [L,R=301]

#html
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.html
RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.html
RewriteRule ^.+\.html$ /%1 [L,R=301]

RewriteRule ^ index.php [L]
<IfModule mod_rewrite.c>
   RewriteEngine on
   RewriteRule ^name(/.*|)$ $1 [L,NC]  
</IfModule>

update: my goal is to make localhost/name/test.php turn in to localhost/test.php

Renoissance
  • 49
  • 1
  • 8

2 Answers2

1

With your shown samples, could you please try following. Please do clear your browser cache before testing your URLs.

<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
##For URLs to be served by .php file in backend. 
RewriteCond %{ENV:REDIRECT_STATUS} ^$   
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]

##For URLs to be served by .html file in backend. 
RewriteCond %{ENV:REDIRECT_STATUS} ^$   
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*?)/?$ $1.html [NC,L]

RewriteRule ^name(/.*|)$ $1 [L,NC]  

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
1

Assuming you don't have a .htaccess inside 123/ subfolder, you may use these rules in site root .htaccess:

RewriteEngine On

# To externally redirect /123/file.php or /123/file.html to /file
RewriteCond %{THE_REQUEST} \s/+123/(.+?)\.(?:php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

# To externally redirect /dir/file.php or /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(?:php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]

# ignore all files and directories from further rules
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

# To internally forward /file to /123/file.php
RewriteCond %{DOCUMENT_ROOT}/123/$1.php -f
RewriteRule ^(.+?)/?$ 123/$1.php [L]

# To internally forward /file to /123/file.html
RewriteCond %{DOCUMENT_ROOT}/123/$1.html -f
RewriteRule ^(.+?)/?$ 123/$1.html [L]

PS: If you already have a .htaccess inside your subfolder 123/ then make sure to remove it before testing these rules.

Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.

anubhava
  • 761,203
  • 64
  • 569
  • 643