Redirect Old URLs with htm Extension [closed] - .htaccess

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have moved to php software that creates SEF URLs. But my site has been around a long time and there are many Not Found errors that come in for old URLs that have the htm extension. Is there a way to direct any URL with the .htm extension to, say, the home page of the site?

This will redirect any URL that ends with .htm or .html to the homepage and if you remove l? it will redirect only .htm.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^(.+?)\.html?$ /? [R=301,L]
Keep in mind if you have other rules on your .htaccess it may conflict with those rules and I would have to see it as the way its done might need to be different to work without conflicting with your current rules.

Related

Why "rsync --exclude-from" ist not working [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Hi I'm trying to sync local files to a remote server, the files are copied, but the excluded files are not working.
What I want to exlude is some files, under some different folders for example:
htdocs/index.php
htdocs/.htaccess
htdocs/conf/Configuration.php
rsync -arvz --no-links --exclude-from 'excluded-files.txt' ./htdocs/ user#host:/var/www/test/htdocs/
What's going wrong?
Unless you are using -R, the exclude patterns are matched against the subdirectories of the from directory. In your case you must exclude index.php rather than htdocs/index.php, but that might match too many such files. By adding -R instead, the name you will test against will be ./htdocs/..., so the patterns will work, but you then need to remove htdocs from the destination directory, i.e. user#host:/var/www/test/

.htaccess code, is this example good coding practice? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Now that my feet are officially wet with .htaccess I have a new question. In one of the many tutorials I have bookmarked, the author recommends placing this bit of code at the beginning of the .htaccess file:
AddDefaultCharset UTF-8
RewriteEngine On
RewriteBase /
I can understand the issues of avoiding parsing issues with that first line, so what I want to know is, aside from RewriteEngine On are the other two lines good practice or just adding bloat?
Many thanks!
The RewriteBase / line is sometimes used to resolve relative URIs in the target and/or patterns of RewriteRule's. For example, if an htaccess file is in the /foo directory, and you have rules that might look something like this:
RewriteEngine On
RewriteBase /foo/
RewriteRule ^bar$ index.php?bar [L]
Here, the rule's target index.php?bar is a relative URI and the base gets prepended to it, resulting in the URI: /foo/index.php?bar. When the rule's target is relative, apache will make a guess as to whether the URI is a file-path or a URL-path, and it doesn't always guess correctly. With the RewriteBase /foo/, apache knows that the /foo/index.php is a URL-path. There could be instances where the rewrite base doesn't match the folder that it's in, but the result is still the same. It acts as a URI base for the relative URI's that are in the rules.
Having it doesn't hurt, unless for some reason you want certain relative URI's to actually map to file-paths.
The AddDefaultCharset directive doesn't apply to the character set of the htaccess file, it's part of the response that the webserver gives to requests when the requested content is of type text/html or text/plain. Again, doesn't hurt to have this in your htaccess file unless you're text or html content is actually some other encoding (like *shift_jis* for example).

mod_rewrite issue or maybe my server is the issue? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I'm having a bit of a fit here. I'm wading into the vast pool of .htaccess in order to rewrite my URLs to be SEO friendly.
I have been researching this all day. I take code snippets from various tutorials and SO posts, yet I get no results and most of the time, no errors.
I have a cPanel dedicated server running Apache 2.0. I called my host, LunarPages, and verified that mod_rewrite was installed and it is, and apparently is working fine.
where I'd like to start is changing the name of index.php to /home/ as in http://www,website.com/home/ making the trailing slash optional as well as home being case insensitive.
Literally, everything I try is doing nothing, it's as though the request is being ignored completely.
I'm writing the .htaccess file in Notepad on my PC and uploading it with FileZilla with the transfer type set to ASCII.
where I'd like to start is changing the name of index.php to /home/ as in http://www,website.com/home/ making the trailing slash optional as well as home being case insensitive.
Try:
RewriteRule ^/?home/?$ /index.php [L,NC]
Note that if you have any relative links (like to scripts or CSS) in index.php and you go to http://www,website.com/home/, the relative links will have /home as a base (instead of /). And you'll either need to make those links absolute or add a URI base to the page header:
<base href="/">

Can anyone think of when a sub-request rewrite is useful?

I found that one of the main things that cause .htaccess rewrite rulesets to do seemingly bizarre things is when Apache decides to try to apply them inside a subrequest. This is to the extent that I now always use the [NS] flag on my rules or use a prefix rule
RewriteCond %{IS_SUBREQ}%{ENV:END} t|1 [NC]
RewriteRule ^ - [L]
(The %{ENV:END} bit just allows me to use E=END:1 to do the same as the V2.4 END flag.)
My Q is: can anyone give me of a good usecase where I wouldn't want to do this? (or alternatively where I would want to use the special -U or -F condition patterns).
I realise that there may be many that I haven't thought of, but the A tick goes to the first valid one.
I'd guess the typical situations where you'd want to apply rewrite rules to subrequests are more or less the same as the one where you'd use symlinks inside your document root.
For a plausible example, let's say you're using Server Side Includes, and have a bunch of files scattered around with suffixes like .html, .shtml and .htm, and perhaps some uppercase variants of these too. At some point, you decide to standardize on the .html suffix, and rename all your files accordingly. But you still have a bunch of legacy code and links that use the other suffixes, and rooting them all out will take a while.
In that case, you might want a rewrite rule like this:
RewriteRule ^(.*)\.s?html?$ $1.html [NC]
By applying this to subrequests too, you ensure that your Server Side Includes don't break because of the renaming.

rewriting url using htaccess [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
I'm pretty new to .htaccess and I'm trying to rewrite the following URL structure:
http://www.example.com/groups/menu.php?id=This is Group Name With Number 25
into:
http://www.example.com/groups/this-is-group-name-with-number-25/
as you can see I need to include - as a separator instead of empty spaces, and I also need to have all characters be lowercase, but allow numbers.
Now since "This is Group Name With Number 25" is user-generated, do I need to make any additional changes within my PHP script so it can generate that kind of URL in web browser or will the .htaccess file handle everything?
You can use external rewriting for such things. This one will be a simple few-line perl script. See apache documentation for "External Rewriting Program".
RewriteMap mapname prg:/path/to/program
You should be handling a path creation like that in PHP, before it even gets sent to the HTML and into an anchor for someone to click on. Not only does it contain spaces which will get converted into %20 and make the URL unreadable, it contains uppercase letters!
There is no efficient way to lowercase your letters and replace all the spaces with dashes efficiently. Most methods will use a bunch of extra HTTP requests.

Resources