How to remove number in any url address in specific folder .htaccess - .htaccess

I want to remove numbers on the end url in specified folder, using htaccess.
(Numbers and minus sign befor numbers). For all urls in this folder.
For example
http://www.example.com/music/new-track-released-52
or
http://www.example.com/music/helo-there-4
Need to look like
http://www.example.com/music/new-track-released
http://www.example.com/music/helo-there
For all links in folder music
(I'm already removed php extension with htaccess)
How to do that?

Probably something like this:
RewriteEngine on
RewriteRule ^/music/(.+)-[0-9]+$ /music/$1
Note that this is the version for the host configuration. For .htaccess style files this has to be slightly modified. Whenever possible you should prefer not to use .htaccess style files but the real host configuration instead. Those files are notoriously error prone, hard to debug and really slow the server down.

Related

I can't find htaccess in typo3

I am using Typo3 4.5., and am running into the problem which appears to be solved here: TYPO3 breaks urls without WWW ... (website redirects to index without WWW)
The answer recommended there involves editing the htaccess file.
My problem is I cannot find this file anywhere. I am not experienced with Typo3, how can I safely edit this document?
I have gone into my filelist and found an htaccess file at [fileadmin/]: temp/ but I cannot edit this document. Clicking on it gives me an error 403 in a new window. In german nonetheless.
Even the question is related to a very old TYPO3 version I want to answer here for the current version 9.5 where several .htaccess files are saved in the folder
typo3/sysext/install/Resources/Private/FolderStructureTemplateFiles/
These are the files in that folder:
fileadmin-temp-htaccess
fileadmin-temp-index.html
fileadmin-user_upload-temp-importexport-htaccess
root-htaccess
root-web-config
typo3temp-var-htaccess
All files with htaccess at the end (above bold text) should be used according to the filename, copied in the corresponding directory and each copy then renamed to .htaccess.
The other files should be used too according to name and content.
It has to be considered that .htaccess-files perhaps are not exchangeable always between TYPO3 versions; I never had problems with it during many updates but it should always be checked.
The .htaccess file is a hidden file and is located in the document root of your website. This file can't be edited from inside of TYPO3, you need direct access to the webserver (ssh, sftp, ...)
If you installed TYPO3 from the "dummy package", it should have a "_.htaccess" file in the root that must be renamed. As the previous answer by #M Klein told you, you must rename or edit it by direct access to your server.
Another possibility is that the file has been accidentally removed; in this case you could download the "dummy package" (select your TYPO3 version) and pick a new one from there.

.htaccess doesn't work if comment row (# ...) exist

code below doesn't work my .htaccess file. I mean, after this code is applied, I can still index folders in html.
# BEGIN disable folder index
Options All -Indexes
# END disable folder index
however, code below works. I mean, after this code is applied, server gives 403 if I try to index a folder which I know that it exists.
Options All -Indexes
I'm on a shared hosting and have nothing to do with server config. .htaccess is created via notepad++ with encoding setting UTF-8 without BOM. .htaccess permission is set to 0644. there exist no other code in .htaccess.
what does this situation mean? what am I doing wrong?
Ok, looks like my original comment above pushed you into the right direction:
Most likely this is a problem with the line breaks. So that for the
interpreting part of the http server that "Options" line is not on a
separate line, thus also commented out. Check your line ending
characters by using a hexeditor. That s the only reliable tool to do
so.

Setting environment specific htaccess rules

So I usually want to set htaccess rules slightly differently based on what server it is on, eg live or development.
The ErrorDocument usually needs to be different, as well as some of the AddType and SetHandlers bits.
Is there any way I can make this a bit more dynamic and get it to auto detect based on the URL, then set a variable and have if conditionals further down in the htaccess?
Want to do this entirely from URL detection instead of setting parameters with apache please :)
No there isn't any way to set those things via some url detection. You can't make normal if conditions surrounding some of the things you want (AddType SetHandlers and ErrorDocument).
You could use env variables and mod rewrite but I don't think you'll like the end result. You'll have to do something like this using env|E=[!]VAR[:VAL] syntax
If you were in the httpd.conf or vhost file you might be able to separate your different setups by using <directory> sections </directory>. But Directory is not allowed in htaccess.
Also I wouldn't do this in a production environment anyways since something could go wrong and I would think the detection is slower and not needed. Perhaps you may want to look into a build script you run to create/deploy your different setups for development/production depending on hostname and other factors.

.htaccess foobar.html -> foobar.php

i have to edit an existing web project. This project has only html pages and links to html pages.
Ex. from foobar.html is a link to bar.html.
Now I have to edit several pages and change the url to bar.php.
I dont want to change every
Put a Redirect directive for each one you need to remap in your .htaccess file.
Redirect /junk.html http://yoursite.com/junk.php permanent
Redirect /junk2.html http://yoursite.com/junk.php permanent
Take a look at sed, awk, python to edit/replace the strings your file. In this order ;)
(see: Unix or Cygwin)

.htaccess or other URL Case Sensitive

My server is Case Sensitive, and id like to turn it to inSensitive.
Example of what I mean is
lets say I upload Fruit.php
Well then going to this file wont work:
www.website.com/fruit.php
but this one will:
www.website.com/Fruit.php
Is there a way so Fruit.php and fruit.php will work? also with the directories. i.e:
/Script/script.php
/script/Script.php
You need to use the mod_speling (sic) apache module:
http://httpd.apache.org/docs/1.3/mod/mod_speling.html
In .htaccess
<IfModule mod_speling.c>
CheckCaseOnly On
CheckSpelling On
</IfModule>
The CheckSpelling operative makes Apache perform a more involved effort to find a match e.g. correcting common spelling mistakes
Case sensitivity depends on the file system, not Apache. There is a partial solution, however. mod_rewrite can coerce everything to lowercase (or uppercase) like so:
RewriteMap tolowercase int:tolower
RewriteRule ^(.*)$ ${tolowercase:$1}
Reference: http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritemap
Unfortunately, this only works if all your files are lowercase, while you specifies mixed case filenames (Fruit.php.) Are you comfortable renaming all the files in your project lowercase?
UNIX-servers are case-sensitive - they distinguish between upper-case and lowercase letters in file names and folder names. So if you move your website from a windows to a UNIX-server (when you change web host for instance), you risk getting a certain amount of "Page not found"-errors (404 errors), because directories and other websites linking to yours sometimes get the cases wrong (typically writing the first letter of folder names in upper-case etc.). This javascript-based custom 404-error page solves the problem by converting URL's into lowercase.
You can get the script from http://www.forbrugerportalen.dk/sider/404casescript.js
Happy coding !!!!!!!

Resources