Removing the last slash of URL in XAMPP - apache

I am currently working a project with PHP and Apache on a local server using XAMPP. I have an issue on my website, I was wondering if I can delete the last slash of my web URL. For example, I'm running a website in localhost and it was stored in folder htacces\web\mywebsite and when I open the website in the browser, the URL bar shows localhost/web/mywebsite/ or 127.0.0.1/web/mywebsite/.
So if this is normal then it's fine, but why can some site like stackoverflow.com remove the last slashes, also like stackoverflow.com\question\ask?
I just want to know about that, is .htaccess will do? or PHP?
here is the .htacces file that I have written in my website
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA]
Could anyone help?

the slash on the end doesnt make much difference. So you just could ignore it, but if you want to allow opening the directories without slash you cant do that in the .htaccess file i think.

Related

htaccess not working on website hosting

I will make this super easy to understand.
On my current pc(home), I have xampp installed and all my files are placed in the root folder. In the root folder, I have an .htaccess file that looks like the following:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profile.php/user.php?id=$1
</IfModule>
Now, I signed up with a host and I copied my whole website and database and everything is perfect except for the htaccess. It doesn't redirect me to profile.php when I type the persons name after the address link
For example: www.website.com/USERNAME
I don't know why it's not working when it works perfectly at home.
The host i'm using is awardspace
On the host, they don't allow you to place your folder in www/ but rather it's like a subdomain like www/folder/yourwebsite
I dont know if thats the problem
I don't even know how to explain.
I just googled "rewrite if not in root(htaccess)" and copy pasted this whole thing
The first answer here: Redirect if file does not exists to root
Modified the directory index to my profile and it worked. 5 freaking hours of searching finally complete. I was about to go to sleep if this last attempt didnt work.

Server is forcing trailing slash on only one URL

My .htaccess has a rewrite that removes .php from the URL so that:
http://example.com/page.php changes to http://example.com/page
This has been working fine for years until I needed to change the name of a file. Now this is happening:
http://example.com/newpage changes to http://example.com/newpage/ and throws a 403 error.
Here's the rewrite that works for everything else:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I'm sure this is some type of caching issue combined with Apache adding a trailing slash by default, but I can't figure out where the caching is happening and I don't want to turn off Apache's trailing slash for security reasons (plus, I shouldn't need to).
Because you'll ask:
I'm using a cPanel VPS with Centos 5. Apache 2.2 and PHP 5.4.
cPanel doesn't appear to have a way to clear the Apache cache. htcacheclean wasn't there and I didn't want to install it in case it screwed up something with cPanel.
Yes, I cleared by browser cache. Tried a new browser. Tried a new computer.
File name is correct. If I change the URL to http://example.com/newpage.php it works fine.
It's been four hours since I made the initial change.
Tried rebooting server.
Any ideas?
To remove a trailing slash using .htaccess you can use the following:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]
It should fix the error you're experiencing.

Why isn't this rewrite rule catching?

I have a website with a bunch of assets using absolute paths, ie:
<img src=\"/_files/images/shell/logo.jpg\">
My issue is, the url for this site in development is: http://arraybox.net/~hazelwoodconstru/
Unfortunately, this means it is trying to access the files at arraybox.net/_files/images/shell/logo.jpg instead of arraybox.net/~hazelwoodconstru/_files/images/shell/logo.jpg
I'm trying to handle this with an .htaccess RewriteRule.
RewriteRule /_files/(.*) /~hazelwoodconstru/_files/$1 [R,NC,L]
I had it working at some point, but I messed around with it, and now it's broken and I can't figure out why!
EDIT:
I also have the following in this .htaccess which may or may not be interfering:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !404\.shtml|500\.shtml|favicon\.ico|index\.php{1}
RewriteRule (.*) /~hazelwoodconstru/ [QSA,L]
You need to make the rule's matching more strict, as well as removing the leading slash:
RewriteRule ^_files/ /~hazelwoodconstru/%{REQUEST_URI} [L,R]
Remove the ,R flag if you don't want an external redirect.
What seems to be happening is that the absolute file paths just plain old won't use arraybox.net/~hazelwoodconstru/filepath - they go straight to arraybox.net/filepath. Writing the .htaccess on the main directory arraybox.net won't work for me, because there will be multiple sites using this setup, and there is no way to tell which /home/ directory the assets really live in.
The solution I went with:
Instead of using the user directory arraybox.net/~hazelwoodconstru, I set up a subdomain
hazelwood.arraybox.net.
I then edited the vhosts config to change the document root for this subdomain to point to the /home/hazelwoodconstru directory (I also had to change the user).
Additionally I had to modify vhosts.local to change phpopenbasedirprotect to use documentroot. Then I had to add allow from all on the /home/ directory.
It's now working on the subdomain.

Trouble with preventing mod_rewrite on a sub folder

I'm trying to set up a small Wordpress install on a sub folder for a client. Their existing site is very small but WP is to be for something separate, so it's not replacing their existing site, it's just to run a separate blog.
Their .htaccess file is as follows
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Their existing site puts everything through the index.php so index.php?contact becomes /contact/
I am trying to put a wordpress install into a subfolder called "blog". However, every time I try to run the installer, I just get the home page. Accessing files like images works fine, but trying to access any .php just loads the home page. I've tried RewriteEngine Off just to test, but it doesn't work.
If I take the above code out, it works (but breaks the site).
My goal is to just have a subdirectory be ignored from the rewrite rules. Any ideas what I'm missing?
Try adding this condition right before the rewrite rule:
RewriteCond %{REQUEST_URI} !^/blog/

.htaccess mod_rewrite issue

Almost in any project I work on, some issues with .htaccess occur. I usually just find the easiest solution and leave it because I don't have any knowledge or understanding for Apache, servers etc. But this time I thought I would ask you guys.
This is the files and folders in my (simplified) setup:
/modrewrite-test
.htaccess
/config
/inc
/lib
/public_html
.htaccess
/cms
/navigation
index.php
edit.php
/pages
index.php
edit.php
login.php
page.php
The "config", "inc" and "lib" folders are meant to be "hidden" from the root of the website. I try to accomplish this by making a .htaccess-file in the root that redirects the user to "public_html". The .htacess-file contains this:
RewriteEngine On
RewriteRule (.*) public_html/$1
This works perfect. If I type "http://localhost/modrewrite-test/login.php" in my browser, I end up in public_html/login.php which is my intention. So this works fine. The .htaccess-file in "public_html" contains this:
RewriteEngine On
# Root
RewriteRule ^$ page.php [L]
# Login
RewriteRule ^(admin)|(login)\/?$ login.php [L]
# Page (if not a file/directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [L]
The first rewrite just redirects me to public_html/page.php if I try to reach "http://localhost/modrewrite-test/". The next rewrite is just for the convenience of users trying to log in - so if they try to reach "http://localhost/modrewrite-test/admin" or "http://localhost/modrewrite-test/login" they will end up at the login.php-file. The third and last rewrite handles the rest of the requests. If I try to reach "http://localhost/modrewrite-test/bla/bla/bla" it will just redirect me to public_html/page.php (with the 'url' GET-variable set) instead of finding a folder called "la", containing a folder named "bla" and etc.
All of these things work perfect but a minor issues occurs when I for instance try to reach "http://localhost/modrewrite-test/cms/navigation" without a slash at the end of the URL. When I try to reach that page the browser is somehow redirected to "http://localhost/modrewrite-test/public_html/cms/navigation/". The correct page is shown but why does it get redirected and add the "public_html" part in the URL? The desired behavior is that the URL stays intact and that the page public_html/cms/navigation/index.php is shown.
The files and folders in the (simplified) can be found at http://highbars.com/modrewrite-test.zip
I ran into the same problem with "strange" redirects when trying to access existing directory without slash at end. In my case this redirection was done by mod_dir Apache module. To disable redirection I used DirectorySlash directive. Try putting in .htaccess files following string:
DirectorySlash Off
RewriteBase may help. Try this in public_html/.htaccess:
RewriteEngine On
RewriteBase /
Add the following to /modrewrite-test/.htaccess:
RewriteBase /modrewrite-test
Just to be on the safe side, I'd add the same rule also to /modrewrite-test/public_html/.htaccess. I found that having RewriteBase always set prevents a lot of potential problems in the future. This however means that you might need to update the values if you change the URI structure of your site.
Update:
I don't think that this is possible with your current folder structure. I believe that the problem is that existing subdirectories prevent rewrite rules from firing. Note the behavior please - everything works fine while you are working with non-existent files and directories, thanks to these two conditions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
However if you try to open any index file from an existing subdirectory, you get redirected to .../public_html/.... Since you can properly open /modrewrite-test/cms/navigation/edit.php, I can only assume that the request is being overwritten by some Apache core directive, which adds slashes at end of folder URLs. Notice that everything works fine if you have an ending-slash at each URL (i.e. the Apache core directory does not need to "correct" your URL, thus everything gets rewritten by your own rewrite rules).
Suggested solution (unless anyone can advise better):
Change /modrewrite-test/public_html/.htaccess as follows:
RewriteEngine On
RewriteBase /modrewrite-test
# Page (if not a file/directory)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ page.php?url=$1 [L]
Then Remove all PHP files from subfolders and use the Front Controller pattern, i.e. route all requests through your main page.php file and do not delegate anything down below.
You can then use the Factory pattern to initiate individual UIs (i.e. navigation/edit.php) directly from your main page.php file based on contents of $_GET['url'] (make sure to properly sanitize that).
Update #2:
This other post on StackOverflow advises on project structure used by Zend Framework - it essentially shows the approach which I suggested above. It is a valuable information asset regardless if you use Zend Framework or not.