3

I want to move from www.example.com/_/randomfolder to example.com/randomfolder. It should acces the contents from /_/randomfolder but should show example.com/randomfolder.

What I have tried:

Trial 1

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,QSA]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Problem: it full fills my requirement from /_/randomfolder but should show example.com/randomfolder but affects my main domain example.com it shows nothing, only a 404 page. I want it to show public_html/index.php only.

Trial 2:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#To load site from  /_/xyz to /xyz  without hcanigng url
RewriteRule ^[A-Za-z0-9._]+  /_/%{REQUEST_URI} [L]

Problem: I have to manually change the address bar from www.example.com/_/randomfolder to example.com/randomfolder, it load contents with or without _ but on first time load its redirects to /_/random folder.

| improve this question | | | | |
  • Why can't you just move the files on your server? Doing that is usually a lot easier than than messing with rewrite rules. – Stephen Ostermiller Mar 18 at 13:34
  • Hi its actually a url shortner , i dont want to spam my main public_html – mrniamster Mar 18 at 14:49
  • 1
    Does that mean you are creating a file for each shortened URL rather than using a database? – Stephen Ostermiller Mar 18 at 16:30
  • "I have to manually change the address bar" - by that do we assume you are still linking to the URL that contains /_? (Your "Trial 1" doesn't have anything to do with this - that's simply a non-www to www and HTTP to HTTPS canonical redirect?) – MrWhite Mar 18 at 16:33
  • @MrWhite yes but whenever user tries to go to link onclick event ,in new tab he see the _/ in it www.xyz.com/_/random he have to manually remove _/ to which still links , – mrniamster Mar 19 at 5:41
1

you are still linking to the URL that contains /_?
... yes but whenever user tries to go to link onclick event ,in new tab he see the _/ in it

Regardless of whether we implement a redirect in .htaccess to "fix" this, you do still need to physically remove the /_ part from the URLs in the HTML source that the users click on. Otherwise, they are not "hidden" from the user (they can see them on the page and in the browser's status bar). Redirecting these URLs will potentially double the number of requests hitting your server and potentially slow the users' experience.

... i dont want to spam my main public_html

Not sure exactly what you mean by "spam", but making the URL appear to be (virtually) in the main directory is really the same as actually being in the main directory - as far as users and search engines are concerned. As far as the local filesystem goes, you still need to avoid conflicts in the same way.

a random folder with index.php inside it it's like this we.xyz.com/_/randomfolder/index.php

In that case, you should be rewriting directly to index.php, not the directory - which would otherwise require mod_dir to issue an internal subrequest for index.php. Also, if you omit the trailing slash then mod_dir would issue an external redirect - which would be undesirable.

Assumptions:

  1. randomfolder can consist of the following characters only: a-z, A-Z, 0-9, _ (underscore), - (hyphen) and . (dot). I've only included the dot, as you appear to have included this in your regex, but otherwise you wouldn't necessarily except directories to contain dots. And omitting the dot would simplify the conditions (since you wouldn't need to check that the request does not match a file).

  2. A request of the form /_/<randomfolder/<anything> strips the trailing /<anything> from the request. And redirects to /<randomfolder> (no trailing slash).

  3. Only requests of the form /<randomfolder> (with or without a trailing slash) are rewritten to /_/<randomfolder>/index.php. We do not currently check that the destination file exists before rewriting (it would result in a 404 either way).

Try the following:

# Redirect requests to remove "/_" from the URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^_/([\w.-]+) /$1 [R=302,L]

# Internally rewrite from "/<folder>" to "/_/<folder>/"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w.-]+)/?$ /_/$1/index.php [L]
| improve this answer | | | | |
  • i tried it , but its its not wroking for me ,i found another solution using javascript lol , here it is : echo "<script>window.history.pushState('', '', '/mrniamster'); </script>" , the problem is that when i use a POST or header location on page it doesnt work idk why i hope we can fix this up – mrniamster Mar 19 at 17:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.