How could I remove slash from a url in .htaccess file? - .htaccess

How could I remove trailing slash in front of a url? For example: stackoverflow.com/hello works on server, which definitely redirects to page named hello, but actually this is not the case on localhost.
However, it ends up something like localhost/hello instead of folder name included in it,Expected result would be localhost/stackoverflow/hello.
<a href="/hello" />
Expected output: localhost/stackoverflow/hello
Output got: localhost/hello
Actually this stuff is working on a live website but not on localhost. o I was looking for some tips to make it this way with .htaccess file.
HTACCESS FILE
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
PS: I have more than 40 files, So I don't think removing "/" from every url makes sense.

Related

htaccess | not looking in the right location for js

I am trying to write a htaccess file that will allow for /example/1/profile to look for a JavaScript file within /example/. Currently on Internet Explorer 11 it is looking for /example/1/file.js whereas realistically it should be looking for /example/file.js.
This needs to be done inside of the .htaccess file as the setup that the website currently has.
I know there is a way in which you can redirect 404 to /example however this is resulting in a 200.
Is their a way I can say in the htaccess file that if it is .js .css to look in /example?
Thanks in advance
EDIT:
For a little but more information, my current htaccess is like this
DirectoryIndex index.php index.html
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /example/index.php [NC,L]
It is for php because the php echos a file_get_contents of the index.html which is an Angular project.
So I need this htaccess to be the following logic
If the file is a .js or .css then rewrite the location to /example else rewrite the location to example/index.php.
The reason this is happening is because I am doing a format which has the ID as a second parameter and for some reason this is interfering with the way that the URL is structured for the js, css.
I imagine this line is what is breaking it...
RewriteRule ^(.*) /example/index.php [NC,L]
Converting my comments to answer. This appears to be problem due to relative links.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/example/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

.htaccess subdomain Rewrite rule is not working

I am making a website builder an I would like to make urls prettier.
The url would be for example:
https://ccc-bb.example.com => https://example.com/project/show/ccc/bb
This is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
# prevents files starting with dot to be viewed by browser
RewriteRule /\.|^\.(?!well-known/) - [F]
# front controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\-(.*)$ https://example.com/project/show/$1/$2 [L]
When I use above (https://ccc-bb.example.com) it sends me to the subdomain empty folder. The folder has only the .htaccess file.
What am I missing? I've never edited an .htaccess file and Google didn't help me (or I don't know what should I looking for).
Your first rule for dotfiles is okay but would be better the other way around, since the second part can only match the start, but the first can only match in subdirectories.
RewriteRule ^\.(?!well-known/)|/\. - [F]
Your other rule's problem is that you are expecting it to match the subdomain. RewriteRules do not operate on the entire string you see in your browser's address bar, only the path part, and in .htaccess they see even less as the leading directory is stripped off, too. To access the info you want, use a RewriteCond:
RewriteCond %{HTTP_HOST} ^([^-]++)-([^-.]++)\.example\.com$
RewriteRule ^(?!project/show/).* project/show/%1/%2/$0 [L,DPI]
(You don't need to include \.example\.com$ if your main domain contains no hyphens.)

.htaccess Rewrite not working without slash in the end

I'm with an issue with .htaccess and I hope you can help me out.
Basically I have the following structure:
root/
+ _archive
+ index.html
+ folder1
+ folder2
+ css
+ ...
On archive I have a few folders that I want it to be accessible through the root of my site.
So let's say my site calls rafael.com, so I would have the following archives:
http://www.rafael.com/_archive/folder10
http://www.rafael.com/_archive/folder20
http://www.rafael.com/_archive/folder30
I want them to be accessible from (without _archive):
http://www.rafael.com/folder10
http://www.rafael.com/folder20
http://www.rafael.com/folder30
But also having the folders and css, and images and etc on my root to keep working. Keep in mind that under folder10, folder20, folder30 I also can have their own images and css, and javascript.
Well, I'm trying the following .htaccess
RewriteEngine On
RewriteCond $1 ^(folder10|folder20|folder30)
RewriteRule ^(.*)$ _archive/$1 [L]
and that works fine if I call using http://www.rafael.com/folder30/ (WITH SLASHES IN THE END) my problem is when I try to call http://www.rafael.com/folder30 without the slashes it gets REDIRECT to http://www.rafael.com/_archive/folder30/ .
So can anyone explains to me WHY it's being redirecting it and how do I fix it in order to have http://www.rafael.com/folder30 and http://www.rafael.com/folder30/ working without redirecting it? :)
Thank you in advanced.
Do you have any other rules in place? without an [R=30x] flag it shouldn't visibly redirect the url. Also you need to fix your RewriteCond $1 to something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^folder(10|20|30)
RewriteRule ^(.*)$ /_archive/$1 [NC,L]

Change Displayed URL Structure using mod_rewrite NOT Working

I need to change the structure of the displayed client-side URL. I'm not too skilled using regex and coding for the .htaccess file. Basically, I have a structure that looks something like:
http://www.example.com/catalog/index.php?cat=lt&sec=lt1-1&id=nmlt10.
I would like this to be displayed in the address bar as:
http://www.example.com/catalog/lt/lt1-1/nmlt10.
This is what I came up with, but it has had no effect:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\$ /catalog/index.php?cat=$1&sec=$2&id=$3 [L]
I tested and removed any other rules in the .htaccess file to ensure nothing was being overwritten. I'm on a shared hosting apache server, and know that mod_rewrite is enabled, because I use it to rewrite non-www to www urls. I don't receive and 500 error messages, I just do not notice any change at all. I'm not sure where I'm going wrong here, so hopefully someone can point me in the right direction.
Finally found a solution that worked:
RewriteEngine On
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
Appreciate LazyOne's response to get me on the right track; however, when using:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I wasn't able to following links that were already placed on the site, it treated different directories as the variables, for example, when browsing to an image or file, say:
folder/folder/image.png
It would grab "folder" - "folder" - and "image" as the variables. I can see why that was happening, if anyone has a different solution or an explanation, please let me know, I'm always willing to learn.
Since your .htaccess is in website root folder, then you should use thus rule:
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/([^/]+)/([^/]+)/([^/]+)$ /catalog/index.php?cat=$1&sec=$2&id=$3 [QSA,L]
If you place it in .htaccess in /catalog/ folder, then you can remove catalog from it:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]
I have tested rule before posting -- works fine for me.
This rule (same as above) will check if URL is a file or folder and will only rewrite if it is not:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)$ index.php?cat=$1&sec=$2&id=$3 [QSA,L]

Htaccess clean urls code not working as I want

So here's what my htaccess clean url code look like:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} !^myvar=0
RewriteCond %{REQUEST_URI} !\..*$
RewriteRule ^([^/]*)/?$ /$1.php?myvar=0 [QSA,L]
This htaccess code permits files on the root to work with .php, trailing slash, or nothing. So blah.com/blah.php works blah.com/blah works blah.com/blah/ works
However if there's a folder on my root, say the name is 'news', the file 'news' on the root cannot take on a clean url. So blah.com/news.php works but blah.com/news and blah.com/news/ both do not work.
For some reason, files inside a folder must have a file extension (.php), so blah.com/news/obama.php works blah.com/news/obama and blah.com/news/obama/ both do not work.
What's wrong with the .htaccess code and how can I correct it?
tl;dr Is there an htaccess code that can make it so .php works, trailing slash works, and no trailing slash works? I know absolutely nothing about htaccess.
Also: I posted this same question yesterday but was met with no reply. Not sure why. If there is something confusing, I'll try to clear it up. Thanks!
Maybe
RewriteBase /
RewriteRule ^(.*)(?=\.php/?)?$ /$1.php?myvar=0 [QSA,L]
could help. Your log files are really golden in this case.

Resources