I am struggeling with something I am trying to do regarding folder. I have a sub-folder called "admin" so I can access the site as mydomain.com/admin.
However I want that if I say mydomain.com/foobar or mydomain.com/28y282djdhjd, then it should always assume it is "admin".
Of course the rule shoud not apply to existing files and folders but it is specifically to the first folder from the domain root.
It's as if I want the admin to be accessed no matter what I call it (as long as it is not a physical folder or file)...
I am not good with htaccess if someone could guide me I would truly appreciate it.
Thank you in advance.
So, you need to:
Check that the request isn't for a file that already exists;
Check that the request isn't for a directory that already exists;
Only redirect the request, if it refers to the first folder-level from the domain root;
If that's all correct, something like this should do it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]*)\/?$ http://example.com/admin [L]
Related
I have this project I maintain which I run in XAMPP and by default the url is set to http://localhost and it will take the folder project name inside htdocs to access the content. For instance I have a folder called test so my base url should be http://localhost/test.
The issue I am facing now is, most of the link was written in something like /profile.php and it will redirect to http://localhost/profile.php instead of http://localhost/test/profile.php. I find it hard to do the testing and all when they redirect to the wrong url.
How do I write the htaccess so that it will always read http://localhost/test as the base url?
This is something I tried but since I still quite confuse with the rule so I was not able to achieve what I'm trying to do.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /test/$1 [R=301,L]
EDIT :
All php files are under test folder at 1st level so they can be accessed by browsing as domain-url/any-file.php. In my case it should be something like http://localhost/test as the domain name and /profile.php as first uri also referring to the file profile.php
I'm working on a new version of a website and I'd like to move the files/folders of the old one to a subdirectory 'old'. Is there a way to keep all links (css, references to "includes" folder and files, etc.) working using a redirect rule, instead of having to manually edit all php files?
Thanks!
You can move all the old files to a directory old and then have a rewrite rule in root .htaccess like this:
RewriteEngine On
RewriteBase /
# if file exists inside old then append /old/ in front of it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/old/$1 -f [NC]
RewriteRule ^(.+?)/?$ old/$1 [L]
No, not as far as i know.
If you use phpstorm you can refactor references easily over all pages, this might fix your problem.
For future projects, it's good practice to use a variable $absolutePath = "C:\...\"
and save the path to the main directory in it, and use that variable in all files.
to link to an image you would do as following:
$imagepath = $absolutepath."subdirectory\image.jpg";
If you move your website you only have to change the one variable.
This is also something you should think about with other frequent used variables like mysql information.
Is it possible to use htaccess to rewrite by checking multiple directories and not just one?
Here is an example of what I am looking for:
I have 2 files
site.com/profile.php?username1
site.com/project.php?projectname1
I would like to make it where the person only has to type in something like:
site.com/NAME -> the "NAME" can be whatever they choose it to be, either the username1 or projectname1, doesn't matter.
Now the issue that I'm having is can htaccess search through both profile AND project to match the name that the user types in and redirect the user to the proper file?
To clarify....if the person types in site.com/username1.....will it automatically know to go to PROFILE.php?username1?
if the person types in site.com/projectname1.....will it automatically know to go to PROJECT.php?projectname1?
You can't use htaccess to do this, because mod_rewrite doesn't know anything about the process of what makes the NAME a profile or a project, only the two php scripts know anything about that. What you're better off doing is writing some routing code in something like router.php or even index.php, then route everything through it:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /router.phpname=$1 [L]
Then in your router.php script, see if the $_GET['name'] is a profile or a project, then route it to either the profile.php or project.php as it sees fit.
I'm still a bit fuzzy on the working of .htaccess, and I've looked around but I can't find anything to help this specific issue.
EDIT: I realize there are other questions that seem like they cover this issue, but I checked some and they didn't seem to offer any help I could understand, and I didn't want to hijack them with my own issues.
This is what I have:
Options +FollowSymLinks
#RewriteBase /
RewriteEngine on
RewriteRule /mp3/(.*) http://old.domain.com/mp3/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
As you can see from the last line, the string typed after the server name is actually a URL parameter and depending on that parameter, different content is pulled from the database and that page is displayed on the site.
The problem I'm having is that the client has a content page called "podcast", so they would go to site.com/podcast which should quietly redirect to site.com/index.php?=podcast and load the content for that page. Unfortunately, the client also has a real site.com/podcast/ folder on their server. Because of this, the rewrite is ignored and the browser attempts to load that folder. It either shows a file listing or a forbidden error if I disable the listing.
After some research (I'm still new to htaccess), I learned that the two lines prior disable the rewrite if the path points to an actual file or folder. Unfortunately, commenting out the one with !-d doesn't seem to have any effect, and commenting out both gives me a server error.
Admittedly, part of the problem here was lack of foresight. URL rewrites should have been planned before everything else was put together, but it wasn't until the site was basically completed that I was notified that the client wants "Friendly URLs" that don't include the ?p= part. Regardless, perhaps there is a way to fix this.
Is there some .htaccess trickery I can use that will force the rewrite even if the URL entered points to a folder (not a specific file) that actually exists? As stated before, removing the !-d doesn't seem to help, although I'm not sure why. Perhaps I misunderstand its purpose.
Thank you for any help, and please be lenient with me if I overlooked something obvious. This is an issue presenting itself on the client's live site right now so I feel a little rushed in solving it. Thanks again.
OH YEAH, and the solution can't be specific to /podcast. The way the client's site is set up, when they want to create a new subpage for the site, a new name is saved for that content based on their title for the page and it is possible (unlikely, but still possible) that another page can be created with a name that matches an existing folder on the server.
Here is a note from mod_rewrite documentation:
By default, mod_rewrite will ignore URLs that map to a directory on
disk but lack a trailing slash, in the expectation that the mod_dir
module will issue the client with a redirect to the canonical URL with
a trailing slash.
This explains why mod_rewrite ignores the URL /podcast. I would suggest that you rename physical directories so that do do not (accidentally) match article names.
Another option would be to disable the DirectorySlash setting. This will prevent Apache from redirecting /podcast to /podcast/.
DirectorySlash Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
Be warned that disabling this setting has side effects. Read through the documentation first.
Change the following line of code:
RewriteRule ^([^/]*)$ /index.php?p=$1 [L]
to
RewriteRule ^(podcast([^?]*)) index.php?p=$1 [L,NC]
I see htaccess questions all over the place but I cant seem to get the thing to do what I want. Im not sure if a redirect or a rewrite is what I need.
Im looking for a way to use a sub folder but make it look there is not a subfolder.
I have a bunch of folders in the root of my domain:
www.mysite.com/folder1
www.mysite.com/folder2
www.mysite.com/folder3
www.mysite.com/folder4
Id like to be able to move folder1 and folder2 to a subfolder but not have that folder visible to the user. For example
www.mysite.com/x/folder1
www.mysite.com/x/folder2
www.mysite.com/folder3
www.mysite.com/folder4
But the user would still see it as it originally was.
It seems like I would have to use a redirect and a rewrite. If a user goes to www.mysite.com/folder1 the will actually be in www.mysite.com/x/folder1 without knowing that they are in that subfolder.
Of course I need to do this without disrupting folder4 and folder5. Ideas? Tips? Tricks? Im not too familiar with htaccess
This is what worked for me, maybe it will help someone else:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1
This is telling the server to ignore the Public folder, so when my URL is actually mysite.com/public/file it shows and responds to mysite.com/file